Repository navigation
Prevent symlink cycle - #7685
Conversation
rainersigwald
left a comment
There was a problem hiding this comment.
I'd rather not introduce something that, if broken, just hangs.
Please write the test. A deterministic test hang is strictly worse than a cleanly failing test but strictly better than a product regression.
| Dictionary<string, List<RecursionState>> searchesToExcludeInSubdirs, | ||
| TaskOptions taskOptions) | ||
| { | ||
| #if NET6_0 |
There was a problem hiding this comment.
As a rule, never use exact-match checks in ifdefs. If you mean "if this API is available" you could say
| #if NET6_0 | |
| #if NET6_0_OR_GREATER |
but the preferred approach is to create a FEATURE_* flag and set it when relevant, checking it here. That's nice because it allows future maintainers to more easily understand the nature of the conditional inclusion: it was like this because LinkTarget was not available on .NET 4.7.2.
There was a problem hiding this comment.
I can certainly do NET6_0_OR_GREATER, but I'm a bit hesitant about a new FEATURE_* for a few reasons. First, making MSBuild logic for "or greater" isn't easy for this sort of thing. I'd have to parse out the version from the target framework then do a version comparison with 6. That messiness is presumably why our other flags that would use NET6_0_OR_GREATER-dependent feature flags use NET6_0_OR_GREATER rather than making new flags, and I do like consistency.
There was a problem hiding this comment.
You can put it in this section:
msbuild/src/Directory.BeforeCommon.targets
Lines 87 to 93 in bfd80ab
There was a problem hiding this comment.
But that isn't sufficient, right? It isn't even available in NET5, according to the docs.
There was a problem hiding this comment.
I know we just use net6, netstandard2.0, and net472, but that feels a little like lying.
There was a problem hiding this comment.
Also maybe true for netstandard2.0?
There was a problem hiding this comment.
This is the standard approach in our codebase. It does not seem important to me to support a theoretical future downgrade to an end-of-life target, and I don't think it's "lying" to say "when building for the current .NET Core TFM, include this code that is present in today's version of that TFM".
| DirectoryInfo baseDirectoryInfo = new(recursionState.BaseDirectory); | ||
| if (baseDirectoryInfo.LinkTarget is not null && baseDirectoryInfo.FullName.Contains(baseDirectoryInfo.LinkTarget)) |
There was a problem hiding this comment.
I think you could make this a little less allocatey:
| DirectoryInfo baseDirectoryInfo = new(recursionState.BaseDirectory); | |
| if (baseDirectoryInfo.LinkTarget is not null && baseDirectoryInfo.FullName.Contains(baseDirectoryInfo.LinkTarget)) | |
| FileSystemInfo linkTarget = Directory.ResolveLinkTarget(recursionState.BaseDirectory, returnFinalTarget: true); | |
| if (linkTarget is not null && baseDirectoryInfo.FullName.Contains(linkTarget.FullName)) |
| // GetFiles in folder C that contains a symlink that points folder D, which contains C as well as other files | ||
| // and most obviously, frameworks other than net6.0 | ||
| // The solution I'd propose for the first two, if necessary, would be maintaining a set of symlinks and verifying, before following it, | ||
| // that we had not followed it previously. The third would require a more involved P/invoke-style fix. |
There was a problem hiding this comment.
Link to #703 and describe this codepath there, please.
| #if NET6_0 | ||
| // This is a pretty quick, simple check, but it misses some cases: | ||
| // symlink in folder A pointing to folder B and symlink in folder B pointing to folder A | ||
| // GetFiles in folder C that contains a symlink that points folder D, which contains C as well as other files |
There was a problem hiding this comment.
I don't understand this case, can you elaborate?
There was a problem hiding this comment.
"If folder C contains file Foo.cs and folder D, and folder D contains a symlink pointing to folder C, calling GetFilesRecursive and passing in folder D would currently find Foo.cs, whereas this would make us miss it."
Is that a clearer description?
That sounds like a valid case, but I feel fine about ignoring it for now, since it would hang with current MSBuild anyway, so we know no one is doing it.
There was a problem hiding this comment.
Ah, I see. Could you avoid the recursion into the "bad" symlink, instead of just returning from it quickly? That feels like the same basic code, but would solve that case (which I think is important).
There was a problem hiding this comment.
How would that help? Either we follow the symlink or we don't, and if we do, then we can only avoid following it repeatedly if we store the visited directories; if we don't, we can't possibly find Foo.cs in that case.
There was a problem hiding this comment.
Given:
- C
- D
- AAA (link to C)
- Foo.cs
- D
With your code, you're saying it would behave like this
- Start at D
- Examine AAA's link status
- return early
With my approach:
- Start at D
- (start foreach child item)
- Examine AAA's link status
- Decide not to recurse into AAA/back to C
- Examine Foo.cs's link status
- Include Foo.cs
This doesn't require storing anything; the "just don't follow symlinks directly up" approach can stand.
There was a problem hiding this comment.
No; with my approach, that scenario would work. It's fairly easy to modify my test to prove that. The scenario that wouldn't work is:
- C
- D
- AAA (link to C)
- Foo.cs
- D
The idea then would be that it would start at D, check AAA, and not follow it, and be finished. Ideally, it would start at D, follow AAA to C, find Foo.cs, check D again, see AAA again but not follow it this time, and stop. That ideal isn't possible now, though, which is why I think this solution is fine.
|
Test seem to be failing because they're essentially passing recursionState.BaseDirectory as "". Is that ever a real case? (It doesn't sound very real to me.) If it is, I'm assuming I can still rely on projectDirectory, though that's also empty in the failing tests. I'm tempted to just disable this change for those tests, but that feels wrong. |
| } | ||
| } | ||
| // This fails in tests with the MockFileSystem when they don't have real paths. | ||
| catch (Exception) { } |
There was a problem hiding this comment.
At least to IOException
Directory.ResolveLinkTarget claims to only throw IOExceptions, but apparently it can also throw ArgumentExceptions. I think it's best to catch both just in case.
Fixes-ish AB#1513887
Context
A symlink in folder A pointing to A will recurse eternally (hang). This fixes that problem in a fairly simple way.
Changes Made
Check if a symlink would take us to a folder above the one we're in before following it.
Testing
I'd rather not introduce something that, if broken, just hangs.