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

Environment variable substitution in cmd scripts #2542

Closed
dbohdan opened this issue Jan 8, 2024 · 5 comments · Fixed by #2544
Closed

Environment variable substitution in cmd scripts #2542

dbohdan opened this issue Jan 8, 2024 · 5 comments · Fixed by #2544
Assignees
Labels
⭐ enhancement Improvements for existing features

Comments

@dbohdan
Copy link

dbohdan commented Jan 8, 2024

Is your feature/enhancement proposal related to a problem? Please describe.

When I tried migrating a project's tasks from Poe the Poet, an independent task runner for Poetry, I ran into a feature that is important to me missing.

Poe can substitute environment variables in cmd tasks, which are like PDM cmd scripts. Here is an example of how I use it:

[tool.poe.env]
"PYTHON_SOURCES" = "src tests"

[tool.poe.tasks.format]
cmd = "ruff format ${PYTHON_SOURCES}"
help = "Run the Ruff formatter"

[tool.poe.tasks.lint]
cmd = "ruff check ${PYTHON_SOURCES}"
help = "Run Ruff"

I find this useful because it allows you to avoid repeating yourself without invoking an external shell. Why shell scripts aren't a sufficient alternative: the shell depends on the platform. On Windows this is still cmd.exe, which has an incompatible syntax for variable substitution.

Describe the solution you'd like

I would like to be able to substitute environment variables as {env:PYTHON_SOURCES} or something similar consistent with existing PDM syntax ({args} and {pdm}) in cmd scripts.

@dbohdan dbohdan added the ⭐ enhancement Improvements for existing features label Jan 8, 2024
@frostming
Copy link
Collaborator

Currently "ruff check ${PYTHON_SOURCES}" also works in PDM cmd, doesn't it work for you?

@dbohdan
Copy link
Author

dbohdan commented Jan 9, 2024

No. Here is a demo of what doesn't work for me.

> cat pyproject.toml
[project]
name = "pdm-test"
version = "0.1.0"
description = "Default template for PDM package"
authors = [
    {name = "D. Bohdan", email = "[email protected]"},
]
dependencies = []
requires-python = "==3.11.*"
readme = "README.md"
license = {text = "MIT"}


[tool.pdm]
package-type = "application"

[tool.pdm.scripts]
_.env = { PYTHON_SOURCES = "test.py" }

foo.shell = "python ${PYTHON_SOURCES}"
bar.cmd = "python ${PYTHON_SOURCES}"

> cat test.py
print("Hello")

> pdm run foo
Hello

> pdm run bar
/home/dbohdan/.local/share/pdm/venvs/pdm-test-q1q4PxnD-3.11/bin/python: can't open file '/tmp/pdm-test/${PYTHON_SOURCES}': [Errno 2] No such file or directory

> pdm --version
PDM, version 2.11.2

@frostming
Copy link
Collaborator

No. Here is a demo of what doesn't work for me.

Okay, the problem is _.env isn't loaded into the current process, but only takes effect in the subprocess

@dbohdan
Copy link
Author

dbohdan commented Jan 9, 2024

The scripts also succeed/fail the same way with

[tool.pdm.scripts]
foo.shell = "python ${PYTHON_SOURCES}"
foo.env = { PYTHON_SOURCES = "test.py" }

bar.cmd = "python ${PYTHON_SOURCES}"
bar.env = { PYTHON_SOURCES = "test.py" }

@frostming
Copy link
Collaborator

if you set env before executing you will find it runs successful

@frostming frostming self-assigned this Jan 9, 2024
frostming added a commit that referenced this issue Jan 9, 2024
frostming added a commit that referenced this issue Jan 10, 2024
* fix: Environment variable substitution in `cmd` scripts
Fixes #2542

Signed-off-by: Frost Ming <[email protected]>

* fix tests

Signed-off-by: Frost Ming <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⭐ enhancement Improvements for existing features
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants