Skip to content

Commit

Permalink
fix null ref caused by build.syncsource=false.
Browse files Browse the repository at this point in the history
  • Loading branch information
TingluoHuang committed Oct 30, 2018
1 parent 3b51642 commit 9cb6ab8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
9 changes: 7 additions & 2 deletions src/Agent.Worker/ContainerOperationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))));
Expand Down
14 changes: 7 additions & 7 deletions src/Agent.Worker/Handlers/PowerShell3Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}

Expand Down
16 changes: 13 additions & 3 deletions src/Agent.Worker/Handlers/StepHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
}

Expand Down

0 comments on commit 9cb6ab8

Please sign in to comment.