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

Throw error if CARGO_TARGET_DIR is an empty string #8939

Merged
merged 6 commits into from
Feb 25, 2021

Conversation

Andy-Python-Programmer
Copy link
Contributor

This pull request makes the target dir to be target/ if CARGO_TARGET_DIR is `` with spaces trimmed and not delete the current project.

Fixes: #8866

@rust-highfive
Copy link

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @ehuss (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 4, 2020
@alexcrichton
Copy link
Member

Thanks for the PR! I'm a bit uncomfortable reinterpreting a request for CARGO_TARGET_DIR, however. It seems like the bug would rather be whomever set this env var because they probably didn't mean for an empty string.

Could this perhaps instead return an error?

@Andy-Python-Programmer Andy-Python-Programmer changed the title Interpret CARGO_TARGET_DIR as target/ if its '' with spaces trimmed Throw error if CARGO_TARGET_DIR is an empty string Dec 5, 2020
@Andy-Python-Programmer
Copy link
Contributor Author

Andy-Python-Programmer commented Dec 5, 2020

Yeup @alexcrichton. It makes sense that if the env variable is empty is probably the users fault. So returning an error makes more sense!

I have made the changes. Now it returns a error:

image

Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

Thanks! Can you add a test for this as well?

Also, is there any reason that we shouldn't handle the other cases set to empty strings as well?

src/cargo/util/config/mod.rs Outdated Show resolved Hide resolved
@ehuss ehuss added S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 12, 2020
@jyn514
Copy link
Member

jyn514 commented Dec 13, 2020

Thanks for the PR! I'm a bit uncomfortable reinterpreting a request for CARGO_TARGET_DIR, however. It seems like the bug would rather be whomever set this env var because they probably didn't mean for an empty string.

Could this perhaps instead return an error?

I would prefer not to do this. The Unix convention is for empty variables to act as if they're unset - if you look at how PAGER and GIT_PAGER behave, it means you can use PAGER= git status as a shorthand for OLD_PAGER=$PAGER; unset PAGER; git status; PAGER=$OLD_PAGER; unset OLD_PAGER;. That second command is awfully long to run.

See also dandavison/delta#386.

@Andy-Python-Programmer
Copy link
Contributor Author

Andy-Python-Programmer commented Dec 14, 2020

I guess then

if dir.to_str()? == "" {
    Ok(Some(Filesystem::new(self.cwd.join("target"))))
}

is the right thing to do. As if we trim it then its actually users mistake and we do not need to check that as when we exe a command like cargo clean then it should return that no dir named {workSpace}/ /

@alexcrichton
Copy link
Member

@jyn514 can you explain when you think the build directory may wish to be configured in a one-off context like that? And why =target would not suffice?

@jyn514
Copy link
Member

jyn514 commented Dec 14, 2020

=target would work, I'd be ok with the error if it suggests doing that instead.

I use this for testing cargo plugins. I have a global CARGO_TARGET_DIR set to an absolute path, which breaks lots and lots of tools that assume target is always the output directory. Either to workaround that breakage, or to test fixes to the command around CARGO_TARGET_DIR, I set the variable one-off a fair bit to make sure this one command has the correct variable without having to rebuild all my other projects now that the target/ directory isn't cached. You can see an example of that here: deadlinks/cargo-deadlinks#66.

@Andy-Python-Programmer
Copy link
Contributor Author

So should I make it return an error, make it behave as if the variable is unset or unset the variable and run the program as normal. @alexcrichton

@alexcrichton
Copy link
Member

@Andy-Python-Programmer the case just below the one you modified, accessing self.build_config()?.target_dir I think should probably also handle the empty-string logic

@Andy-Python-Programmer
Copy link
Contributor Author

Andy-Python-Programmer commented Jan 11, 2021

Andy-Python-Programmer the case just below the one you modified, accessing self.build_config()?.target_dir I think should probably also handle the empty-string logic

@alexcrichton I am not sure as we should only handle the case when the user has set the env var. Cause target will be "" only when the user sets it to be "" right?

@alexcrichton
Copy link
Member

I'm personally not sure why we would only handle the case of the env var. Why would the env var require special handling but not other methods of configuration?

@Andy-Python-Programmer
Copy link
Contributor Author

Why would the env var require special handling but not other methods of configuration?

Because we cannot set the config variable in the other methods of config to `` I guess.

@alexcrichton
Copy link
Member

Sorry I'm not really understanding your responses. Let me try to be more clear.

If you set export CARGO_TARGET_DIR=, then this PR will generate an error next time you build.

My point is that if you set:

[build]
target-dir = '' 

or export CARGO_BUILD_TARGET_DIR=, thten this PR should also generate an error. Currently it does not.

@Andy-Python-Programmer
Copy link
Contributor Author

Ah yes that makes sense. I will just do that 🙃

@Andy-Python-Programmer
Copy link
Contributor Author

Andy-Python-Programmer commented Jan 23, 2021

Now if you set

[build]
target-dir = '' 

or CARGO_TARGET_DIR to "", you will get an error error: the target directory is set to an empty string. Is that more like it? I also added tests.

@alexcrichton
Copy link
Member

Could you be sure to add a test for the config case and a test for the env case as well?

@joshtriplett
Copy link
Member

Following up on this: I think this is a good idea, and I think we should merge this as soon as we have tests for the other cases.

@Andy-Python-Programmer
Copy link
Contributor Author

Just a question =>

#[cargo_test]
fn cargo_target_empty_env() {
    let config = ConfigBuilder::new().env("CARGO_TARGT_DIR", "").build();

for the env test I think this is the right way to set the env in the config. So will I need to add a case to also check self.env?

@alexcrichton
Copy link
Member

It may make more sense to have more of an "integration test" for this where you build a whole project and execute cargo build. That way you can simulate the env var with .env(...) when building the subprocess and the config version can be emulated by creating a file.

@Andy-Python-Programmer
Copy link
Contributor Author

All good.

@Andy-Python-Programmer
Copy link
Contributor Author

Andy-Python-Programmer commented Feb 24, 2021

Now:

Case 1

If you set the environment variable to "". It will error out saying:
error: the target directory is set to an empty string in the CARGO_TARGET_DIR environment variable.

Case 2

[build]
target-dir = '' 

If you set the target diectory to "" in the config file it will error out saying:
error: the target directory is set to an empty string in the config.toml file.

I hope I did this right this time :)

src/cargo/util/config/mod.rs Outdated Show resolved Hide resolved
src/cargo/util/config/mod.rs Outdated Show resolved Hide resolved
@alexcrichton
Copy link
Member

@bors: r+

Thanks!

@bors
Copy link
Collaborator

bors commented Feb 25, 2021

📌 Commit b5b2e48 has been approved by alexcrichton

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author. labels Feb 25, 2021
@bors
Copy link
Collaborator

bors commented Feb 25, 2021

⌛ Testing commit b5b2e48 with merge 439a2016b7306bfe948a788f89607e8cceb7876a...

@bors
Copy link
Collaborator

bors commented Feb 25, 2021

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Feb 25, 2021
@alexcrichton
Copy link
Member

@bors: retry

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 25, 2021
@bors
Copy link
Collaborator

bors commented Feb 25, 2021

⌛ Testing commit b5b2e48 with merge f3e63d6...

@bors
Copy link
Collaborator

bors commented Feb 25, 2021

☀️ Test successful - checks-actions
Approved by: alexcrichton
Pushing f3e63d6 to master...

@bors bors merged commit f3e63d6 into rust-lang:master Feb 25, 2021
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Mar 3, 2021
Update cargo

12 commits in 572e201536dc2e4920346e28037b63c0f4d88b3c..c68432f1e5cbbc09833699a951b1b5b059651dff
2021-02-24 16:51:20 +0000 to 2021-03-02 18:26:29 +0000
- Don't panic when printing JSON with non-utf8 paths (rust-lang/cargo#9226)
- Detect changes for JSON spec targets. (rust-lang/cargo#9223)
- Fix `cargo_target_empty_cfg` test with env var. (rust-lang/cargo#9225)
- Correct default cargo new edition (rust-lang/cargo#9202)
- Update split-debuginfo docs around the default. (rust-lang/cargo#9224)
- Minor update to registry API error messages. (rust-lang/cargo#9213)
- Some minor code cleanup. (rust-lang/cargo#9214)
- doc: Fix spelling worksapce->workspace (rust-lang/cargo#9212)
- Update SPDX version in docs. (rust-lang/cargo#9209)
- Throw error if CARGO_TARGET_DIR is an empty string (rust-lang/cargo#8939)
- testsuite: Use split debuginfo on macos. (rust-lang/cargo#9207)
- testsuite: Improve performance when using rustup. (rust-lang/cargo#9206)
@ehuss ehuss added this to the 1.52.0 milestone Feb 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cargo clean ate my entire project
7 participants