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

feat: set project root env #3072

Closed
wants to merge 6 commits into from

Conversation

ZeeD
Copy link

@ZeeD ZeeD commented Aug 2, 2024

Pull Request Checklist

  • A news fragment is added in news/ describing what is new.
  • Test cases added for changed code.

Describe what you have changed in this PR.

Expose PROJECT_ROOT as an env variable during script runs

fixes #2993

@frostming
Copy link
Collaborator

Why not just inject the env into the process_env when running processes?

process_env.update(project_env.process_env)

You won't contaminate the global_options in this way.

@frostming
Copy link
Collaborator

Oh, the project root is already exposed via PDM_PROJECT_ROOT env.

return {"PATH": new_path, "PDM_PROJECT_ROOT": str(project.root)}

If no objection, i will close this PR.

@ZeeD
Copy link
Author

ZeeD commented Aug 2, 2024

Why not just inject the env into the process_env when running processes?

process_env.update(project_env.process_env)

You won't contaminate the global_options in this way.

Yeah, I may try this approach instead

Oh, the project root is already exposed via PDM_PROJECT_ROOT env.

return {"PATH": new_path, "PDM_PROJECT_ROOT": str(project.root)}

If no objection, i will close this PR.

I didn't knew about PDM_PROJECT_ROOT, I may have missed it in the docs, sorry.

But, while invoking directly a script I see that PDM_PROJECT_ROOT is available, it seems to me that in the .env variable expansion it doesn't work: let's say that I have

pyproject.toml:

[tool.pdm.scripts]
_.env_file = ".env"
var1 = { shell = "echo $VAR1" }
var2 = { shell = "echo $VAR2" }
var3 = { shell = "echo $PDM_PROJECT_ROOT" }

.env

VAR1 = "VAL1"

VAR2 = "VAR1=${VAR1}; USER=${USER}; PDM_PROJECT_ROOT=${PDM_PROJECT_ROOT};"

pdm run have some unexpected behaviour:

$ pdm run var1
VAL1
$ pdm run var2
VAR1=VAL1; USER=<my username>; PDM_PROJECT_ROOT=;
$ pdm run var3
<current directory
$ 

it seems that the definition of the PDM_PROJECT_ROOT env variable occurs "too late" to let dotenv enrich the .env file parsing.

Do you think it would be possible to move the PDM_PROJECT_ROOT before the evaluation of the _.env file during the execution of the run?

@frostming
Copy link
Collaborator

frostming commented Aug 2, 2024

it seems that the definition of the PDM_PROJECT_ROOT env variable occurs "too late" to let dotenv enrich the .env file parsing.

Not only that, all additional env vars are not injected into os.environ before spawning the child process.

@ZeeD
Copy link
Author

ZeeD commented Aug 2, 2024

searching for PDM_PROJECT_ROOT I've found #1324
Do you think that my use case - as you can see in #2993 - could be solved changing the behavior of pdm?

@frostming
Copy link
Collaborator

This is superseded by #3087 thanks anyway

@frostming frostming closed this Aug 8, 2024
@ZeeD
Copy link
Author

ZeeD commented Aug 8, 2024

thanks to you for the update!

@ZeeD ZeeD deleted the feat/set_project_root_env branch August 8, 2024 07:15
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.

dinamically set env variable
2 participants