Skip to content

Commit a076ffc

Browse files
authored
Merge pull request #21996 from michaelnebel/csharp/fixpathcombineissues
C#: Fix the `cs/path-combine` code quality issues in the extractor.
2 parents f65d1e8 + 03b525b commit a076ffc

31 files changed

Lines changed: 76 additions & 71 deletions

File tree

csharp/autobuilder/Semmle.Autobuild.CSharp.Tests/BuildScripts.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ IEnumerable<string> IBuildActions.EnumerateFiles(string dir)
135135
if (!EnumerateFiles.TryGetValue(dir, out var str))
136136
throw new ArgumentException("Missing EnumerateFiles " + dir);
137137

138-
return str.Split("\n").Select(p => PathCombine(dir, p));
138+
return str.Split("\n").Select(p => PathJoin(dir, p));
139139
}
140140

141141
public IDictionary<string, string> EnumerateDirectories { get; } = new Dictionary<string, string>();
@@ -147,7 +147,7 @@ IEnumerable<string> IBuildActions.EnumerateDirectories(string dir)
147147

148148
return string.IsNullOrEmpty(str)
149149
? Enumerable.Empty<string>()
150-
: str.Split("\n").Select(p => PathCombine(dir, p));
150+
: str.Split("\n").Select(p => PathJoin(dir, p));
151151
}
152152

153153
public bool IsWindows { get; set; }
@@ -170,7 +170,7 @@ IEnumerable<string> IBuildActions.EnumerateDirectories(string dir)
170170

171171
bool IBuildActions.IsMonoInstalled() => IsMonoInstalled;
172172

173-
public string PathCombine(params string[] parts)
173+
public string PathJoin(params string[] parts)
174174
{
175175
return string.Join(IsWindows ? '\\' : '/', parts.Where(p => !string.IsNullOrWhiteSpace(p)));
176176
}

csharp/autobuilder/Semmle.Autobuild.CSharp/DotNetRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public static BuildScript WithDotNet(IAutobuilder<AutobuildOptionsShared> builde
109109
=> WithDotNet(builder, ensureDotNetAvailable: false, (_, env) => f(env));
110110

111111
private static string DotNetCommand(IBuildActions actions, string? dotNetPath) =>
112-
dotNetPath is not null ? actions.PathCombine(dotNetPath, "dotnet") : "dotnet";
112+
dotNetPath is not null ? actions.PathJoin(dotNetPath, "dotnet") : "dotnet";
113113

114114
private static CommandBuilder GetCleanCommand(IBuildActions actions, string? dotNetPath, IDictionary<string, string>? environment)
115115
{

csharp/autobuilder/Semmle.Autobuild.Cpp.Tests/BuildScripts.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ IEnumerable<string> IBuildActions.EnumerateDirectories(string dir)
158158

159159
bool IBuildActions.IsMonoInstalled() => IsMonoInstalled;
160160

161-
string IBuildActions.PathCombine(params string[] parts)
161+
string IBuildActions.PathJoin(params string[] parts)
162162
{
163163
return string.Join(IsWindows ? '\\' : '/', parts.Where(p => !string.IsNullOrWhiteSpace(p)));
164164
}

csharp/autobuilder/Semmle.Autobuild.Shared/Autobuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public abstract class Autobuilder<TAutobuildOptions> : IDisposable, IAutobuilder
108108
/// </summary>
109109
/// <param name="path">The relative path.</param>
110110
/// <returns>True iff the path was found.</returns>
111-
public bool HasRelativePath(string path) => HasPath(Actions.PathCombine(RootDirectory, path));
111+
public bool HasRelativePath(string path) => HasPath(Actions.PathJoin(RootDirectory, path));
112112

113113
/// <summary>
114114
/// List of project/solution files to build.

csharp/autobuilder/Semmle.Autobuild.Shared/BuildTools.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static IEnumerable<VcVarsBatFile> GetCandidateVcVarsFiles(IBuildActions a
3232
yield break;
3333

3434
// Attempt to use vswhere to find installations of Visual Studio
35-
var vswhere = actions.PathCombine(programFilesx86, "Microsoft Visual Studio", "Installer", "vswhere.exe");
35+
var vswhere = actions.PathJoin(programFilesx86, "Microsoft Visual Studio", "Installer", "vswhere.exe");
3636

3737
if (actions.FileExists(vswhere))
3838
{
@@ -51,14 +51,14 @@ public static IEnumerable<VcVarsBatFile> GetCandidateVcVarsFiles(IBuildActions a
5151
if (majorVersion < 15)
5252
{
5353
// Visual Studio 2015 and below
54-
yield return new VcVarsBatFile(actions.PathCombine(vsInstallation.InstallationPath, @"VC\vcvarsall.bat"), majorVersion);
54+
yield return new VcVarsBatFile(actions.PathJoin(vsInstallation.InstallationPath, @"VC\vcvarsall.bat"), majorVersion);
5555
}
5656
else
5757
{
5858
// Visual Studio 2017 and above
59-
yield return new VcVarsBatFile(actions.PathCombine(vsInstallation.InstallationPath, @"VC\Auxiliary\Build\vcvars32.bat"), majorVersion);
60-
yield return new VcVarsBatFile(actions.PathCombine(vsInstallation.InstallationPath, @"VC\Auxiliary\Build\vcvars64.bat"), majorVersion);
61-
yield return new VcVarsBatFile(actions.PathCombine(vsInstallation.InstallationPath, @"Common7\Tools\VsDevCmd.bat"), majorVersion);
59+
yield return new VcVarsBatFile(actions.PathJoin(vsInstallation.InstallationPath, @"VC\Auxiliary\Build\vcvars32.bat"), majorVersion);
60+
yield return new VcVarsBatFile(actions.PathJoin(vsInstallation.InstallationPath, @"VC\Auxiliary\Build\vcvars64.bat"), majorVersion);
61+
yield return new VcVarsBatFile(actions.PathJoin(vsInstallation.InstallationPath, @"Common7\Tools\VsDevCmd.bat"), majorVersion);
6262
}
6363
}
6464
// else: Skip installation without a version
@@ -68,10 +68,10 @@ public static IEnumerable<VcVarsBatFile> GetCandidateVcVarsFiles(IBuildActions a
6868
}
6969

7070
// vswhere not installed or didn't run correctly - return legacy Visual Studio versions
71-
yield return new VcVarsBatFile(actions.PathCombine(programFilesx86, @"Microsoft Visual Studio 14.0\VC\vcvarsall.bat"), 14);
72-
yield return new VcVarsBatFile(actions.PathCombine(programFilesx86, @"Microsoft Visual Studio 12.0\VC\vcvarsall.bat"), 12);
73-
yield return new VcVarsBatFile(actions.PathCombine(programFilesx86, @"Microsoft Visual Studio 11.0\VC\vcvarsall.bat"), 11);
74-
yield return new VcVarsBatFile(actions.PathCombine(programFilesx86, @"Microsoft Visual Studio 10.0\VC\vcvarsall.bat"), 10);
71+
yield return new VcVarsBatFile(actions.PathJoin(programFilesx86, @"Microsoft Visual Studio 14.0\VC\vcvarsall.bat"), 14);
72+
yield return new VcVarsBatFile(actions.PathJoin(programFilesx86, @"Microsoft Visual Studio 12.0\VC\vcvarsall.bat"), 12);
73+
yield return new VcVarsBatFile(actions.PathJoin(programFilesx86, @"Microsoft Visual Studio 11.0\VC\vcvarsall.bat"), 11);
74+
yield return new VcVarsBatFile(actions.PathJoin(programFilesx86, @"Microsoft Visual Studio 10.0\VC\vcvarsall.bat"), 10);
7575
}
7676

7777
/// <summary>

csharp/autobuilder/Semmle.Autobuild.Shared/MsBuildRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public BuildScript Analyse(IAutobuilder<AutobuildOptionsShared> builder, bool au
6060
// Use `nuget.exe` from source code repo, if present, otherwise first attempt with global
6161
// `nuget` command, and if that fails, attempt to download `nuget.exe` from nuget.org
6262
var nuget = builder.GetFilename("nuget.exe").Select(t => t.Item1).FirstOrDefault() ?? "nuget";
63-
var nugetDownloadPath = builder.Actions.PathCombine(FileUtils.GetTemporaryWorkingDirectory(builder.Actions.GetEnvironmentVariable, builder.Options.Language.UpperCaseName, out _), ".nuget", "nuget.exe");
63+
var nugetDownloadPath = builder.Actions.PathJoin(FileUtils.GetTemporaryWorkingDirectory(builder.Actions.GetEnvironmentVariable, builder.Options.Language.UpperCaseName, out _), ".nuget", "nuget.exe");
6464
var nugetDownloaded = false;
6565

6666
var ret = BuildScript.Success;

csharp/autobuilder/Semmle.Autobuild.Shared/Project.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,9 @@ public Project(Autobuilder<TAutobuildOptions> builder, string path) : base(build
107107
continue;
108108
}
109109

110-
var includePath = builder.Actions.PathCombine(include.Value.Split('\\', StringSplitOptions.RemoveEmptyEntries));
111-
ret.Add(new Project<TAutobuildOptions>(builder, builder.Actions.PathCombine(DirectoryName, includePath)));
110+
var includePath = builder.Actions.PathJoin(include.Value.Split('\\', StringSplitOptions.RemoveEmptyEntries));
111+
var path = Path.IsPathRooted(includePath) ? includePath : builder.Actions.PathJoin(DirectoryName, includePath);
112+
ret.Add(new Project<TAutobuildOptions>(builder, path));
112113
}
113114
return ret;
114115
});

csharp/autobuilder/Semmle.Autobuild.Shared/Solution.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public Solution(Autobuilder<TAutobuildOptions> builder, string path, bool allowP
7979

8080
includedProjects = solution.ProjectsInOrder
8181
.Where(p => p.ProjectType == SolutionProjectType.KnownToBeMSBuildFormat)
82-
.Select(p => builder.Actions.PathCombine(DirectoryName, builder.Actions.PathCombine(p.RelativePath.Split('\\', StringSplitOptions.RemoveEmptyEntries))))
82+
.Select(p => builder.Actions.PathJoin(DirectoryName, builder.Actions.PathJoin(p.RelativePath.Split('\\', StringSplitOptions.RemoveEmptyEntries))))
8383
.Select(p => new Project<TAutobuildOptions>(builder, p))
8484
.ToArray();
8585
}

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyContainer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void Add(string package, string dependency)
5050
return;
5151
}
5252

53-
var path = Path.Combine(p, ParseFilePath(d));
53+
var path = Path.Join(p, ParseFilePath(d));
5454
Paths.Add(path);
5555
Packages.Add(GetPackageName(p));
5656
}

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public DependencyManager(string srcDir, ILogger logger)
7575
}
7676
}
7777

78-
this.diagnosticsWriter = new DiagnosticsStream(Path.Combine(
78+
this.diagnosticsWriter = new DiagnosticsStream(Path.Join(
7979
diagDirEnv ?? "",
8080
$"dependency-manager-{DateTime.UtcNow:yyyyMMddHHmm}-{Environment.ProcessId}.jsonc"));
8181
this.sourceDir = new DirectoryInfo(srcDir);
@@ -327,7 +327,7 @@ private void AddNetFrameworkDlls(ISet<AssemblyLookupLocation> dllLocations, ISet
327327
private void RemoveNugetPackageReference(string packagePrefix, ISet<AssemblyLookupLocation> dllLocations)
328328
{
329329
var packageFolder = nugetPackageRestorer.PackageDirectory.DirInfo.FullName.ToLowerInvariant();
330-
var packagePathPrefix = Path.Combine(packageFolder, packagePrefix.ToLowerInvariant());
330+
var packagePathPrefix = Path.Join(packageFolder, packagePrefix.ToLowerInvariant());
331331
var toRemove = dllLocations.Where(s => s.Path.StartsWith(packagePathPrefix, StringComparison.InvariantCultureIgnoreCase));
332332
foreach (var path in toRemove)
333333
{

0 commit comments

Comments
 (0)