Skip to content

feat(cli): Added --color styling flag to wkg cli#220

Merged
mkatychev merged 18 commits into
mainfrom
mkatychev/wkg-add-colour
Jul 2, 2026
Merged

feat(cli): Added --color styling flag to wkg cli#220
mkatychev merged 18 commits into
mainfrom
mkatychev/wkg-add-colour

Conversation

@mkatychev

Copy link
Copy Markdown
Member

Added --color styling flag to wkg cli:

$ ./target/debug/wkg --help
Usage: wkg [OPTIONS] <COMMAND>

Commands:
  ...
Options:
      --color <WHEN>  Controls when to use color [default: auto] [possible values: auto, always, never]
  ...

Added warnln!, statusln!, and helpln! (stolen™ from cargo):
image

@mkatychev mkatychev requested a review from vados-cosmonic June 30, 2026 23:01
@mkatychev

This comment was marked as outdated.

@mkatychev mkatychev changed the title Mkatychev/wkg add colour feat(cli): Added --color styling flag to wkg cli Jun 30, 2026

@vados-cosmonic vados-cosmonic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not against using anstyle for this, but what do you think about using tracing w/ tracing_subscriber::fmt's ANSI color layer? That would give us error/warn/info colors, and be more interoperable with tracing in the libs.

@mkatychev

mkatychev commented Jun 30, 2026

Copy link
Copy Markdown
Member Author

I'm not against using anstyle for this, but what do you think about using tracing w/ tracing_subscriber::fmt's ANSI color layer? That would give us error/warn/info colors, and be more interoperable with tracing in the libs.

I've thought about that as well, when we get to exposing wkg as a lib, I would rather have switch to tracing as a detection layer similar to how cargo's [Shell::print] does it:
https://github.com/rust-lang/cargo/blob/a595d0da21f228b7fdae64d3d5c0e527ea66bb59/crates/cargo-util-terminal/src/shell.rs#L79-L96

that flexibility needs to be built to and should come with a GlobalContext analogue as well (which we need for better workspace support as well).

I think for now it makes sense to distinguish between purely human stuff (no tracing wrapper) and things triggered by RUST_LOG, ultimately all the anstyle stuff should either be turned off or turned into tracing::event! calls when used as a lib.

@mkatychev mkatychev changed the base branch from mkatychev/refactor/move-wkg-wit-top to main June 30, 2026 23:44
@mkatychev mkatychev force-pushed the mkatychev/wkg-add-colour branch from d96658a to 3572fe5 Compare June 30, 2026 23:48
Comment thread crates/wasm-pkg-common/src/package.rs Outdated
Comment thread crates/wasm-pkg-common/src/package.rs Outdated
Comment thread crates/wasm-pkg-core/src/resolver.rs Outdated
Comment thread crates/wkg/src/main.rs
@mkatychev mkatychev force-pushed the mkatychev/wkg-add-colour branch from 273bd2e to 8569654 Compare July 1, 2026 18:28
Comment thread crates/wkg/src/main.rs
@mkatychev mkatychev force-pushed the mkatychev/wkg-add-colour branch from 7751a71 to 467ac5d Compare July 1, 2026 18:33
@mkatychev mkatychev force-pushed the mkatychev/wkg-add-colour branch from 467ac5d to d620cc7 Compare July 1, 2026 18:36
@mkatychev mkatychev requested a review from vados-cosmonic July 1, 2026 18:37

@vados-cosmonic vados-cosmonic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for taking another pass at this, we're almost there.

Comment thread crates/wasm-pkg-core/Cargo.toml Outdated
Comment thread crates/wkg/src/main.rs
Comment thread crates/wkg/src/main.rs
@mkatychev mkatychev requested a review from vados-cosmonic July 2, 2026 01:30
Comment thread Cargo.toml

[workspace.package]
edition = "2021"
edition = "2024"

@mkatychev mkatychev Jul 2, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Tried seeing if bumping would resolve previous test failures:
https://github.com/bytecodealliance/wasm-pkg-tools/actions/runs/28563549597/job/84686098906

Updating edition did not fix issues and this is out of scope but a nice to have regardless.

This was actual fix:

# running `cargo test` (implicit `--workspace` flag), leaks `ansi-term-output` from wkg
test = []

TLDR: resolver 3 (and 2) bleeds wkg feature flags when running cargo test from the workspace.

cargo test -p wasm-pkg-core is not impacted but it is impractical to invoke explicitly all the time - hence "test" feature flag for ergonomics.

@mkatychev mkatychev Jul 2, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

alternative: we can remove the ansi styling completely from the types but regaining functionality (if worth it) will not be feasible short term and the workspace feature bleed will still be a problem for other flags.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

To try and restate the problem here -- was it that cargo test -p wkg (or regular cargo test of the workspace) dependency-unifies to wasm-pkg-core *with* ansi-term-outputenabled, no matter what, because of the dep w/ features at thewkg` level?

If so, IMO I don't think we even need to introduce the test feature, adding cargo test -p wasm-pkg-core --no-default-features or something to CI to make sure it runs without is enough.

We could even pull in something like cargo-hack for CI in the future, but my concern here is more that we just get it covered under test so we can't put out a broken release/merge broken code to main.

It's a style thing but what do you think about defining the dep and it's transitive flag dependencies explicitly in wkg too, but as a default?

I believe the syntax would be something like:

[features]
defaults = ["ansi-term-output"]
ansi-term-output = [ "wasm-pkg-common/ansi-term-output", "wasm-pkg-core/ansi-term-output"]

and then we could make the ansi-term output deps optional like the other lib crates (though they are pulled in by default)

If someone for example wanted to build the binary even smaller, for some reason, they could run cargo install wkg --no-default-features and get a non-ansi-term build this way, rather than the dependencies being required.

I think this pattern is pretty well accepted, as far as optional deps go, even for bin crates, but I'm OK if you don't want to do this either.

@mkatychev mkatychev force-pushed the mkatychev/wkg-add-colour branch from f5ded81 to 0fe596d Compare July 2, 2026 05:29

@vados-cosmonic vados-cosmonic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM 🚀

Comment thread Cargo.toml

[workspace.package]
edition = "2021"
edition = "2024"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

To try and restate the problem here -- was it that cargo test -p wkg (or regular cargo test of the workspace) dependency-unifies to wasm-pkg-core *with* ansi-term-outputenabled, no matter what, because of the dep w/ features at thewkg` level?

If so, IMO I don't think we even need to introduce the test feature, adding cargo test -p wasm-pkg-core --no-default-features or something to CI to make sure it runs without is enough.

We could even pull in something like cargo-hack for CI in the future, but my concern here is more that we just get it covered under test so we can't put out a broken release/merge broken code to main.

It's a style thing but what do you think about defining the dep and it's transitive flag dependencies explicitly in wkg too, but as a default?

I believe the syntax would be something like:

[features]
defaults = ["ansi-term-output"]
ansi-term-output = [ "wasm-pkg-common/ansi-term-output", "wasm-pkg-core/ansi-term-output"]

and then we could make the ansi-term output deps optional like the other lib crates (though they are pulled in by default)

If someone for example wanted to build the binary even smaller, for some reason, they could run cargo install wkg --no-default-features and get a non-ansi-term build this way, rather than the dependencies being required.

I think this pattern is pretty well accepted, as far as optional deps go, even for bin crates, but I'm OK if you don't want to do this either.

@mkatychev

mkatychev commented Jul 2, 2026

Copy link
Copy Markdown
Member Author

If so, IMO I don't think we even need to introduce the test feature, adding cargo test -p wasm-pkg-core --no-default-features or something to CI to make sure it runs without is enough.

From a UX perspective, I feel it is important for a default argument set to behave predictably and avoid external dependencies as much as possible (especially for top level subcommands like * build, * check, and * test), it is why this section bothers me so much:

[features]
default = ["docker-tests"]
# This feature enables tests that use Docker
docker-tests = []

I would rather deal with awkward design choices in the margins than rely on callouts & caveats sprinkled everywhere to push the user towards providing a specific set of parameters or lean on external tooling like docker, cargo-hack, bash, or just to scaffold baseline expectations.

This is obviously subjective but a user should be surprised with a failure when they run something like cargo test on a repo after a fresh clone.

Comment thread crates/wkg/src/oci.rs
into_auth_should_read_docker_registry_credentials();
into_auth_should_other_registry_credentials();
std::env::remove_var("DOCKER_CONFIG");
// NOT-SAFE: this is likely causing race conditions

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I run into race conditions on darwin arm very often with these tests, @ryan-surname-p have you had the same experience?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't recall having issues with this but that's the fun thing about race conditions I could have just gotten lucky.

That said this is definitely racy which it seems someone has acknowledged above on line 235 as well.

Wrapping it in unsafe does make it much more clear.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

the unsafe was a hard error thanks to the 2024 edition bump!

@vados-cosmonic

Copy link
Copy Markdown
Contributor

Yeah unfortunately I think how Rust work has our hands tied here, given these facts:

  1. Using the ansi-term-output feature in wkg pulls in extra crates
  2. ansi-term-output isn't actually required, but we want it to be enabled by default

I'd like to have this be really obvious from reading the Cargo.toml.

Separately, the docker-tests feature should not exist (IMO, like the test feature shouldn't) -- the most elegant way I've seen that navigated is discovering those features in build.rs and injecting a dynamic cfg or ENV that lets the tests do what they need to do. I think the current code was an initial approach at this that should be removed.

As far as what the user (developer) tests/has to configure, there is no actual change here based on what I'm describing, it's purely notational, except for the user losing the ability to build without those features required. Requiring those features without giving an opt out is the same as having it as a default feature, except slightly worse because the user has no way to do a more minimal build.

The onus to test both the default build and the minimal build is on us as maintainers IMO -- the average developer need not do it.

@mkatychev

Copy link
Copy Markdown
Member Author

@vados-cosmonic thanks for the explanation on #220 (comment)

@vados-cosmonic

Copy link
Copy Markdown
Contributor

@mkatychev Oh no worries, the build.rs feature detection thing is it's a pattern I've seen others do and shamlessly stole 😆

It's funny how every time something just-barely-uncomfortable like this comes up I go search on rust-lang/rust and find some 5+ year old issue where someone is asking for the same thing :)

@mkatychev

Copy link
Copy Markdown
Member Author

I think I fell for the classic scope creep trap in this PR, I will revert the core/common changes and merge without the display impls and submit a separate PR

@mkatychev mkatychev merged commit 03dfe9b into main Jul 2, 2026
6 checks passed
@mkatychev mkatychev deleted the mkatychev/wkg-add-colour branch July 2, 2026 14:52
mkatychev added a commit that referenced this pull request Jul 2, 2026
Added `--color` styling flag to wkg cli:
```sh-session
$ ./target/debug/wkg --help
Usage: wkg [OPTIONS] <COMMAND>

Commands:
  ...
Options:
      --color <WHEN>  Controls when to use color [default: auto] [possible values: auto, always, never]
  ...

```

* Added `warnln!`, `statusln!`, and `helpln!` macros
mkatychev added a commit that referenced this pull request Jul 2, 2026
Added `--color` styling flag to wkg cli:
```sh-session
$ ./target/debug/wkg --help
Usage: wkg [OPTIONS] <COMMAND>

Commands:
  ...
Options:
      --color <WHEN>  Controls when to use color [default: auto] [possible values: auto, always, never]
  ...

```

* Added `warnln!`, `statusln!`, and `helpln!` macros
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.

3 participants