Skip to content

Commit 4c9bd4a

Browse files
author
John Luo
authored
Warn if file reference not resolved (#412)
1 parent 35574ea commit 4c9bd4a

File tree

4 files changed

+43
-11
lines changed

4 files changed

+43
-11
lines changed

src/dotnet-grpc/Commands/CommandBase.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,17 +253,30 @@ internal string[] GlobReferences(string[] references)
253253
var directoryToSearch = Path.GetPathRoot(reference)!;
254254
var searchPattern = reference.Substring(directoryToSearch.Length);
255255

256-
expandedReferences.AddRange(Directory.GetFiles(directoryToSearch, searchPattern));
256+
var resolvedFiles = Directory.GetFiles(directoryToSearch, searchPattern);
257+
258+
if (resolvedFiles.Length == 0)
259+
{
260+
Console.LogWarning(CoreStrings.LogWarningNoReferenceResolved, reference);
261+
}
262+
263+
expandedReferences.AddRange(resolvedFiles);
257264
continue;
258265
}
259266

260267
if (Directory.Exists(Path.Combine(Project.DirectoryPath, Path.GetDirectoryName(reference)!)))
261268
{
269+
var resolvedFiles = Directory.GetFiles(Project.DirectoryPath, reference);
270+
271+
if (resolvedFiles.Length == 0)
272+
{
273+
Console.LogWarning(CoreStrings.LogWarningNoReferenceResolved, reference);
274+
}
275+
262276
expandedReferences.AddRange(
263-
Directory.GetFiles(Project.DirectoryPath, reference)
264-
// The reference is relative to the project directory but GetFiles returns the full path.
265-
// Remove the project directory portion of the path so relative references are maintained.
266-
.Select(r => r.Replace(Project.DirectoryPath + Path.DirectorySeparatorChar, string.Empty)));
277+
// The reference is relative to the project directory but GetFiles returns the full path.
278+
// Remove the project directory portion of the path so relative references are maintained.
279+
resolvedFiles.Select(r => r.Replace(Project.DirectoryPath + Path.DirectorySeparatorChar, string.Empty)));
267280
}
268281
}
269282

src/dotnet-grpc/Properties/CoreStrings.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/dotnet-grpc/Properties/CoreStrings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,7 @@
207207
<data name="ErrorOutputMustBeFilePath" xml:space="preserve">
208208
<value>Output path `{0}` is invalid. The path cannot be a directory path and must be a file path.</value>
209209
</data>
210+
<data name="LogWarningNoReferenceResolved" xml:space="preserve">
211+
<value>No file found matching file argument `{0}`. File reference not added.</value>
212+
</data>
210213
</root>

test/dotnet-grpc.Tests/CommandBaseTests.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using Grpc.Dotnet.Cli.Commands;
2424
using Grpc.Dotnet.Cli.Internal;
2525
using Grpc.Dotnet.Cli.Options;
26+
using Grpc.Dotnet.Cli.Properties;
2627
using Grpc.Tests.Shared;
2728
using Microsoft.Build.Definition;
2829
using Microsoft.Build.Evaluation;
@@ -318,33 +319,39 @@ public void ResolveServices_ReturnsClient_IfNotWebSDK()
318319
}
319320

320321
[Test]
321-
public void GlobReferences_ExpandsRelativeReferences()
322+
public void GlobReferences_ExpandsRelativeReferences_WarnsIfReferenceNotResolved()
322323
{
323324
// Arrange
325+
var testConsole = new TestConsole();
324326
var commandBase = new CommandBase(
325-
new TestConsole(),
327+
testConsole,
326328
CreateIsolatedProject(Path.Combine(Directory.GetCurrentDirectory(), "TestAssets", "EmptyProject", "test.csproj")));
329+
var invalidReference = Path.Combine("Proto", "invalid*reference.proto");
327330

328331
// Act
329-
var references = commandBase.GlobReferences(new[] { Path.Combine("Proto", "*.proto") });
332+
var references = commandBase.GlobReferences(new[] { Path.Combine("Proto", "*.proto"), invalidReference });
330333

331334
// Assert
332335
Assert.Contains(Path.Combine("Proto", "a.proto"), references);
333336
Assert.Contains(Path.Combine("Proto", "b.proto"), references);
337+
Assert.AreEqual($"Warning: {string.Format(CoreStrings.LogWarningNoReferenceResolved, invalidReference, SourceUrl)}", testConsole.Out.ToString()!.TrimEnd());
334338
}
335339

336340
[Test]
337-
public void GlobReferences_ExpandsAbsoluteReferences()
341+
public void GlobReferences_ExpandsAbsoluteReferences_WarnsIfReferenceNotResolved()
338342
{
339343
// Arrange
340-
var commandBase = new CommandBase(new TestConsole(), new Project());
344+
var testConsole = new TestConsole();
345+
var commandBase = new CommandBase(testConsole, new Project());
346+
var invalidReference = Path.Combine(Directory.GetCurrentDirectory(), "TestAssets", "EmptyProject", "Proto", "invalid*reference.proto");
341347

342348
// Act
343-
var references = commandBase.GlobReferences(new[] { Path.Combine(Directory.GetCurrentDirectory(), "TestAssets", "EmptyProject", "Proto", "*.proto") });
349+
var references = commandBase.GlobReferences(new[] { Path.Combine(Directory.GetCurrentDirectory(), "TestAssets", "EmptyProject", "Proto", "*.proto"), invalidReference });
344350

345351
// Assert
346352
Assert.Contains(Path.Combine(Directory.GetCurrentDirectory(), "TestAssets", "EmptyProject", "Proto", "a.proto"), references);
347353
Assert.Contains(Path.Combine(Directory.GetCurrentDirectory(), "TestAssets", "EmptyProject", "Proto", "b.proto"), references);
354+
Assert.AreEqual($"Warning: {string.Format(CoreStrings.LogWarningNoReferenceResolved, invalidReference, SourceUrl)}", testConsole.Out.ToString()!.TrimEnd());
348355
}
349356

350357
static object[] DirectoryPaths =

0 commit comments

Comments
 (0)