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

fix: unset PRJ_ROOT to prevent impure behavior #220

Merged
merged 1 commit into from
Aug 12, 2024

Conversation

GaetanLepage
Copy link
Contributor

@GaetanLepage GaetanLepage commented Aug 11, 2024

When the PRJ_ROOT environment variable is set, treefmt will use it to populate the --tree-root option.
Also, both --tree-root and --tree-root-file options must not be set at the same time, otherwise leading to:

treefmt: error: --tree-root and --tree-root-file can't be used together

As the tree-fmt module is explicitly setting --tree-root-file, I don't see a justification to let PRJ_ROOT leak in this script.

@GaetanLepage
Copy link
Contributor Author

To give a bit more context, I faced this issue while using devshell which automatically sets the $PRJ_ROOT environment variable.

The combination of devshell and treefmt-nix thus leads to the aforementioned error message.

@emilazy
Copy link
Contributor

emilazy commented Aug 11, 2024

cc #219

I think the flake-parts module may still be broken after this, cf.

options.projectRoot = lib.mkOption {
type = types.path;
default = self;
defaultText = lib.literalExpression "self";
description = ''
Path to the root of the project on which treefmt operates
'';
};
.

@emilazy
Copy link
Contributor

emilazy commented Aug 11, 2024

Or maybe not? projectRoot is only used for the check.

Anyway I was thinking that making projectRootFile optional might be a good idea too.

@GaetanLepage
Copy link
Contributor Author

Well, I don't know where this should be fix.
But the formatting script, as it is generated right now, should definitely ignore the PRJ_ROOT variable. At best, the latter is not set and thus ignored, but at worst, it generates the error.

Of course, there might be a cleaner solution.

@emilazy
Copy link
Contributor

emilazy commented Aug 11, 2024

Right. The potential alternative, I guess, is to make projectRootFile optional and omit --tree-root-file when PRJ_ROOT is set. Not sure what would be best.

(This needs rebasing, by the way, I think.)

@MattSturgeon
Copy link

The potential alternative, I guess, is to make projectRootFile optional and omit --tree-root-file when PRJ_ROOT is set. Not sure what would be best.

I don't see why we would want impure environment variables leaking through to treefmt? Isn't it possible that PRJ_ROOT could end up being set to some arbitrary value that is unrelated to what was configured in the treefmt-nix flake module? The environment shouldn't be able to override something explicitly configured in a module and defined in the wrapper script.

@emilazy
Copy link
Contributor

emilazy commented Aug 11, 2024

Well, I think searching for the project root is kind of inherently impure to begin with. But sure, that’s fair enough. (projectRoot being an option that’s only used for the flake check, rather than setting --tree-root similarly to projectRootFile, is pretty confusing in itself, but that’s another matter.)

@zimbatm
Copy link
Member

zimbatm commented Aug 12, 2024

This is a bug in treefmt. Env vars should not take precedence over CLI args.

@zimbatm
Copy link
Member

zimbatm commented Aug 12, 2024

alecthomas/kong#446

@zimbatm
Copy link
Member

zimbatm commented Aug 12, 2024

I'll merge this for now (given upstream doesn't think this is an issue)

@zimbatm zimbatm merged commit 349de7b into numtide:main Aug 12, 2024
242 checks passed
@GaetanLepage GaetanLepage deleted the project-root branch August 12, 2024 10:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants