-
Notifications
You must be signed in to change notification settings - Fork 4
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
Running a command in a subdir which needs file paths that are absolute or relative to the subdir #37
Comments
I've been thinking a fair bit recently about the
And for all of these I think there is a variant where we do this but we treat a single subdirectory as the root. It sounds like for your particular use case you want #3 with the "subdirectory as the new root" option. Is that correct? I've been thinking of replacing Off-hand, I think something like this might work; invoke = { "once" | "per-dir" | "per-file" }
run-in = { "root" | "dir" }
file_args = { "none" | "absolute-dir" | "absolute-file" | "relative-dir" | "relative-file" }
with-root = "path/to/subdir" Though I think this has some nonsensical combos, like |
This is an interesting matrix. 😄 I think for my case I'd be looking at "Run the command once per file, chdir into that file's directory passing the absolute path to that file".
So, I think those configuration options would cover it nicely. I think they're also clearer than |
I realized that we only need three keys, since If you have any feedback on this I'd appreciate it. |
Great! A couple of thoughts:
This looks really good. I think it would cover my use cases. |
I have a build of the new code up at https://github.com/houseabsolute/precious/actions/runs/3358319122 Would you be up for giving it a try? You can see the new docs in the invoke branch. Also see new the new docs dir there. |
Yes. I will test drive it today and let you know. Thanks for this! |
Just had a quick look and a couple of things.
[command.some-linter]
invoke = "per-file"
working_dir = {
"sub_roots" = [
"pkg1",
"pkg2",
]
}
path_args = "file" It works when changed to [command.some-linter]
invoke = "per-file"
working_dir = { "sub_roots" = [ "pkg1","pkg2", ] }
path_args = "file" (Not sure why). Also, I'm not sure if the new options get me the invocation that I need. I want
[commands.black]
type = "both"
lint_flags = "--check"
invoke = "per-file"
working_dir = { "sub_roots" = [ "pylib" ] }
path_args = "absolute-file"
cmd = ["pipenv", "--bare", "--quiet", "run", "black"]
ignore_stderr = [".*reformatted.*", "would reformat.*", ".* left unchanged",]
ok_exit_codes = 0
include = [ "**/*.py" ] The error message that I get back is:
That's an error that I get if I try to run the command outside of the I checked and apparently we're abusing |
Oh, I think there's a better way to write this, which is: working_dir.sub_roots = [
"pkg1",
"pkg2",
] I suspect the way I first wrote it doesn't work because TOML doesn't allow all the line breaks.
The config you gave should do what you want. Can you run |
Doh, I just realized that a bunch of integration tests I thought were running were actually a no-op. So I don't think this code actually works yet! |
I re-ran the command with |
Alright, now I think it might actually work. I realized I hadn't even implemented the sub-roots code at all before! https://github.com/houseabsolute/precious/actions/runs/3405255971 |
This works for everything in my The remaining issue is that I have some python scripts in folders outside of
and the config: [commands.black]
type = "both"
lint_flags = "--check"
invoke = "per-file"
working_dir = { "sub_roots" = [ "pylib" ] }
path_args = "absolute-file"
cmd = ["pipenv", "--bare", "--quiet", "run", "black"]
ignore_stderr = [".*reformatted.*", "would reformat.*", ".* left unchanged",]
ok_exit_codes = 0
include = [ "**/*.py" ]
exclude = [
"ansible/**/*",
"web/**/*",
]
I don't see a way to do this currently. I do understand that this is probably a non-standard use case, but if there were a way to say "get all the matching files and just run all of the commands from this arbitrary root", that would cover this case. |
Yeah, there's no way to do that even with the new options. I think one way that might work is to add an option that looks like this: working_dir.root = "pylib" I don't think this would be too hard to add. But I do wonder how likely anyone else is to have this use case. It seems like you could easily write a small shell wrapper for |
That's what I was thinking. 🤔
Right. I've got a few variations of: #!/bin/bash
set -eu -o pipefail
BASE=$PWD
# This script is a wrapper used by precious. It exists as there doesn't seem to
# be an easy way to make precious change to an arbitrary working dir before
# running a command.
cd pylib
if [[ ${#@} -gt 1 ]]; then
PIPENV_VERBOSITY=-1 pipenv --bare --quiet run \
black "$1" "$BASE/$2"
else
PIPENV_VERBOSITY=-1 pipenv --bare --quiet run \
black "$BASE/$1"
fi It was just a bit of a pain to do it |
It does occur to me that this exposes something about the new config options that is less than ideal. Basically, these are the things we want to control.
So 1-3 correspond to So maybe it'd make sense to add a fourth config item, called something like |
Thinking about it more, I wonder if instead of |
And yet another option: working_dir = { sub_roots = "pylib", include_all_files = true } That would be fairly easy to add and I don't think it'd make understanding the config options too complicated. |
Nice! Either of those options would work for me. I can't currently think of a reason for needing multiple sub_roots, but there are a lot of linters out there that I'm not familiar with. |
After thinking about this a bit I realized that the whole Given that, I replaced it with |
Please try out the new build at https://github.com/houseabsolute/precious/actions/runs/3452598158 and let me know. |
I tested it out yesterday and this works really well. |
I just released v0.4.0 with this change (and a few others) in it. |
I've got a configuration for black which is kind of interesting. The command needs to be run from inside a
./pylib
directory because ofvirtualenv
. We've worked around this by having a wrapper script which willprecious
calls. That script willcd pylib
before runningblack
.However, if we do this,
precious
hands off paths with are prefixedpylib/
andblack
cannot find the files. The next step would be to turn the relative URL into an absolute. So I end up with something like:If
black
is run as a linter, it needs a--check
arg, so the wrapper (clumsily) deals with the fact that a switch could precede the filename.I don't know if there's an elegant way to deal with this in
precious
. If there were options to haveprecious
:then I think it would cover this case. FWIW I'm going to have to do something similar for
mypy
andpylint
, so I thought it was worth raising here.The text was updated successfully, but these errors were encountered: