diff --git a/src/Agent.Worker/ContainerOperationProvider.cs b/src/Agent.Worker/ContainerOperationProvider.cs index bddba38ce0..0affe03cab 100644 --- a/src/Agent.Worker/ContainerOperationProvider.cs +++ b/src/Agent.Worker/ContainerOperationProvider.cs @@ -177,8 +177,13 @@ public async Task StartContainerAsync(IExecutionContext executionContext, object container.MountVolumes.Add(new MountVolume(HostContext.GetDirectory(WellKnownDirectory.Work), container.TranslateToContainerPath(HostContext.GetDirectory(WellKnownDirectory.Work)))); container.MountVolumes.Add(new MountVolume(HostContext.GetDirectory(WellKnownDirectory.Tools), container.TranslateToContainerPath(HostContext.GetDirectory(WellKnownDirectory.Tools)))); #else - string workingDirectory = Path.GetDirectoryName(executionContext.Variables.Get(Constants.Variables.System.DefaultWorkingDirectory).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)); - container.MountVolumes.Add(new MountVolume(container.TranslateToHostPath(workingDirectory), workingDirectory)); + string defaultWorkingDirectory = executionContext.Variables.Get(Constants.Variables.System.DefaultWorkingDirectory); + if (!string.IsNullOrEmpty(defaultWorkingDirectory)) + { + string workingDirectory = Path.GetDirectoryName(defaultWorkingDirectory.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)); + container.MountVolumes.Add(new MountVolume(container.TranslateToHostPath(workingDirectory), workingDirectory)); + } + container.MountVolumes.Add(new MountVolume(HostContext.GetDirectory(WellKnownDirectory.Temp), container.TranslateToContainerPath(HostContext.GetDirectory(WellKnownDirectory.Temp)))); container.MountVolumes.Add(new MountVolume(HostContext.GetDirectory(WellKnownDirectory.Tools), container.TranslateToContainerPath(HostContext.GetDirectory(WellKnownDirectory.Tools)))); container.MountVolumes.Add(new MountVolume(HostContext.GetDirectory(WellKnownDirectory.Tasks), container.TranslateToContainerPath(HostContext.GetDirectory(WellKnownDirectory.Tasks)))); diff --git a/src/Agent.Worker/Handlers/PowerShell3Handler.cs b/src/Agent.Worker/Handlers/PowerShell3Handler.cs index d0e9640f0c..96767f54f5 100644 --- a/src/Agent.Worker/Handlers/PowerShell3Handler.cs +++ b/src/Agent.Worker/Handlers/PowerShell3Handler.cs @@ -60,13 +60,13 @@ public async Task RunAsync() // A non-zero exit code indicates infrastructural failure. // Task failure should be communicated over STDOUT using ## commands. await StepHost.ExecuteAsync(workingDirectory: StepHost.ResolvePathForStepHost(scriptDirectory), - fileName: powerShellExe, - arguments: powerShellExeArgs, - environment: Environment, - requireExitCodeZero: true, - outputEncoding: null, - killProcessOnCancel: false, - cancellationToken: ExecutionContext.CancellationToken); + fileName: StepHost.ResolvePathForStepHost(powerShellExe), + arguments: powerShellExeArgs, + environment: Environment, + requireExitCodeZero: true, + outputEncoding: null, + killProcessOnCancel: false, + cancellationToken: ExecutionContext.CancellationToken); } diff --git a/src/Agent.Worker/Handlers/StepHost.cs b/src/Agent.Worker/Handlers/StepHost.cs index 52515efeb3..3991463671 100644 --- a/src/Agent.Worker/Handlers/StepHost.cs +++ b/src/Agent.Worker/Handlers/StepHost.cs @@ -91,14 +91,13 @@ public string ResolvePathForStepHost(string path) // remove double quotes around the path path = path.Trim('\"'); - // try to resolve path inside container if the request path is part of the mount volume - // otherwise just return the file name and rely on the file is part of the %PATH% inside the container. #if OS_WINDOWS if (Container.MountVolumes.Exists(x => path.StartsWith(x.SourceVolumePath, StringComparison.OrdinalIgnoreCase))) #else if (Container.MountVolumes.Exists(x => path.StartsWith(x.SourceVolumePath))) #endif { + // try to resolve path inside container if the request path is part of the mount volume return Container.TranslateToContainerPath(path); } #if OS_WINDOWS @@ -107,11 +106,22 @@ public string ResolvePathForStepHost(string path) else if (Container.MountVolumes.Exists(x => path.StartsWith(x.TargetVolumePath))) #endif { + // skip container path resolve, since the request path is already a path inside the container return path; } else { - return Path.GetFileName(path); + var containerPath = Container.TranslateToContainerPath(path); + if (!string.Equals(containerPath, path, StringComparison.Ordinal)) + { + // the request path is not part of the mount volume, but it might match some path in the folder structure. + return containerPath; + } + else + { + // otherwise just return the file name and rely on the file is part of the %PATH% inside the container. + return Path.GetFileName(path); + } } }