Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove use of Environment.CurrentDirectory #69564

Merged
merged 1 commit into from
Aug 17, 2023
Merged

Conversation

jaredpar
Copy link
Member

The compiler should not make use of Environment.CurrentDirectory in the libraries layer. That layer cannot depend on anything environment related because doing so would break cases like VBCSCompiler which needs to be environment agnostic.

The one use was in AdditionalSourcesCollection to verify that the provided hint name was within the current working directory. After some thought I decided the best course was to remove this restriction. The ratoinale is once you actually thread through the current directory you find that it must be added to the Compilation. Effectively everything that hosts a Compilation must provide a working directory and that goes against the goals of Compilation which seeks to be agnostic to working directories.

Think allowing customers to specify the full paths was the lesser evil here.

closes #69516

The compiler should not make use of `Environment.CurrentDirectory` in
the libraries layer. That layer cannot depend on anything environment
related because doing so would break cases like VBCSCompiler which needs
to be environment agnostic.

The one use was in `AdditionalSourcesCollection` to verify that the
provided hint name was within the current working directory. After some
thought I decided the best course was to remove this restriction. The
ratoinale is once you actually thread through the current directory you
find that it must be added to the `Compilation`. Effectively everything
that hosts a `Compilation` must provide a working directory and that
goes against the goals of `Compilation` which seeks to be agnostic to
working directories.

Think allowing customers to specify the full paths was the lesser evil
here.

closes dotnet#69516
@jaredpar jaredpar requested a review from a team as a code owner August 16, 2023 21:47
@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged Issues and PRs which have not yet been triaged by a lead label Aug 16, 2023
@jaredpar
Copy link
Member Author

@dotnet/roslyn-compiler PTAL
@chsienki want to get your thoughts on this

@chsienki
Copy link
Contributor

So I believe the original intention behind this was to stop people from essentially doing hintNames like ..\..\..\foo.cs or whatever to 'escape' from the obj\generated directory when writing to disk is enabled.

We probably don't need that, if a generator wants to be malicious it can just File.WriteAllText anyway, so it's not a security boundary or anything, just encouraging authors to be good citizens.

I think its probably fine to just remove it, or we could potentially add \..\ as a pattern that isn't allowed in hintNames, but it's probably not worth it.

@@ -84,11 +83,6 @@ public void Add(string hintName, SourceText source)
throw new ArgumentException(string.Format(CodeAnalysisResources.SourceTextRequiresEncoding, hintName), nameof(source));
}

if (Path.IsPathRooted(hintName) || !Path.GetFullPath(hintName).StartsWith(Environment.CurrentDirectory, _hintNameComparison))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could potentially keep the Path.IsPathRooted check if we think its worth it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we keep it though and fire and error then we potentially break existing code. 😦

Copy link
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM Thanks (iteration 1)

@jaredpar jaredpar enabled auto-merge (squash) August 16, 2023 23:14
@jaredpar jaredpar merged commit 1686899 into dotnet:main Aug 17, 2023
25 checks passed
@ghost ghost added this to the Next milestone Aug 17, 2023
@jaredpar jaredpar deleted the env branch August 18, 2023 20:42
@dibarbet dibarbet modified the milestones: Next, 17.8 P2 Aug 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Compiler should not use Environment type
4 participants