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

Remove hard-coded support for mainargs.Leftover/Flag/SubParser to support alternate implementations #62

Merged
merged 18 commits into from
Apr 29, 2023

Conversation

lihaoyi
Copy link
Member

@lihaoyi lihaoyi commented Apr 27, 2023

This PR moves the handling of mainargs.Leftover/Flag/SubParser from a hard-coded ArgSig that only works with mainargs.Leftover or mainargs.Flag, to properties of the TokensReader that can be configured to work with any custom type.

Should probably be reviewed concurrently with com-lihaoyi/mill#1948, which is the motivation for this PR: we want to be able to define a CLI entrypoing taking mill.define.Task[mainargs.Leftover[T]] or equivalent, which is currently impossible due to the hard-coded nature of mainargs.Leftover (and mainargs.Flag etc.)

Major Changes

  1. ArgReader is eliminated and ArgSig is greatly simplified to a single type with no subtypes or type parameters

  2. TokensReader is split into 5primary sub-types - .Simple, Constant, .Flag, .Leftover, and .Class. These roughly mirror the original {ArgSig,ArgReader}.{Simple,Flag,Leftover,Class} case classes. The 5 sub-classes control behavior through Renderer.scala/Invoker.scala/TokensGrouping.scala in the same way.

The major effect of moving the logic from {ArgSig,ArgReader} to TokensReader is that they now are no longer hard-coded to work with mainargs.{Flag,Leftover,Subparser} types. Now, anyone who has a custom type Foo can choose whether they want to define a TokensReader.Simple for it, or whether they want to define a TokensReader.Leftover or TokensReader.Flag. Similarly, people can define their own TokensReader.Class rather than relying on the default implementation in mainargs.ParserForClass.

Testing

Tested with two new flavors of VarargsTests (now renamed VarargsBasedTests:

  1. VarargsWrappedTests that exercises using a custom wrapper type to define a main entrypoints that takes Wrapper[mainargs.Leftover[T]],

  2. VarargsCustomTests that replaces mainargs.Leftover[T] entirely and defines main entrypoints that take Wrapper[T]

  3. Added a ConstantTests.scala to exercise the code path, which was previously the noTokens codepath and un-tested in this repo

  4. All existing tests pass

Notes

  1. I chose to remove the type params from ArgSig because they weren't really paying for their complexity; most of the time we were passing around ArgSig[_, _]s anyway, so we weren't getting type safety, but they nevertheless caused tons of headaches trying to get types to line up. The un-typed default: Option[Any => Any], reader: TokensReader[_] isn't great, but it's a lot easier to work with and TBH not really much less type-safe than the status quo

  2. Because ArgSig and TokensReader now have a circular dependency on each other (via TokensReader.Class -> MainData -> ArgSig -> TokensReader), I moved them into the same file. This both makes the acyclic linter happy, and also kind of makes sense since they're now part of the same recursive data structure (v.s. previously ArgSig was the recursive data structure with TokensReaders hanging off of each node)

  3. The new structure with TokensReader as a sealed trait with 5 distinct sub-types is different from what it was before, with TokensReader as a single class with a grab-bag of all possible fields and callbacks. I thought the sealed trait approach is much cleaner here, since they reflect exactly the data necessary 4 different scenarios we care about, whereas otherwise we'd find some fields meaningless in some cases e.g. Flag has no meaningful fields, Leftover doesn't care about noTokens or alwaysRepeatable or allowEmpty, etc.

@lihaoyi lihaoyi changed the title [WIP] try to remove hard-coded support for mainargs.Leftover to support alternate implementations [WIP] try to remove hard-coded support for mainargs.Leftover/Flag/SubParser to support alternate implementations Apr 27, 2023
@lihaoyi lihaoyi changed the title [WIP] try to remove hard-coded support for mainargs.Leftover/Flag/SubParser to support alternate implementations Remove hard-coded support for mainargs.Leftover/Flag/SubParser to support alternate implementations Apr 27, 2023
@lihaoyi lihaoyi requested review from lolgab and lefou April 27, 2023 14:22
@lihaoyi
Copy link
Member Author

lihaoyi commented Apr 28, 2023

Rebased this on top of https://github.com/com-lihaoyi/mill

I renamed isHidden to just hidden for consistency with other flags like positional=true

Copy link
Member

@lefou lefou left a comment

Choose a reason for hiding this comment

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

Looks ok to me. I just glanced over all that macro stuff, but tests are green, what should happen. ;)

build.sc Outdated
@@ -19,7 +19,7 @@ val scalaJSVersions = scalaVersions.map((_, "1.10.1"))
val scalaNativeVersions = scalaVersions.map((_, "0.4.7"))

trait MainArgsPublishModule extends PublishModule with CrossScalaModule with Mima {
def publishVersion = VcsVersion.vcsState().format()
def publishVersion = "0.5.0-M1"
Copy link
Member

Choose a reason for hiding this comment

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

Why is that? Once you tag the commit, it should happen automatically.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh that's just leftover from manual testing using publishLocal, will revert before landing

Copy link
Member Author

Choose a reason for hiding this comment

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

reverted

@lihaoyi lihaoyi merged commit 3298647 into main Apr 29, 2023
3 of 4 checks passed
@lihaoyi lihaoyi deleted the softcode-leftover branch April 29, 2023 00:32
lihaoyi added a commit that referenced this pull request Apr 29, 2023
…#66)

Should fix #58 and
#60

Previously, we allowed any arg to take positional arguments if
`allowPositional = true` (which is the case for Ammonite and Mill
user-defined entrypoints.), even `mainargs.Flag`s. for which being
positional doesn't make sense.

```scala
    val positionalArgSigs = argSigs
      .filter {
        case x: ArgSig.Simple[_, _] if x.reader.noTokens => false
        case x: ArgSig.Simple[_, _] if x.positional => true
        case x => allowPositional
      }
```

The relevant code path was rewritten in
#62, but the buggy behavior
was preserved before and after that change. This wasn't caught in other
uses of `mainargs.Flag`, e.g. for Ammonite/Mill's own flags, because
those did not set `allowPositional=true`

This PR tweaks `TokenGrouping.groupArgs` to be more discerning about how
it selects positional, keyword, and missing arguments:

1. Now, only `TokenReader.Simple[_]`s with `.positional` or
`allowPositional` can be positional; `Flag`s, `Leftover`, etc. cannot

2. Keyword arguments are limited only to `Flag`s and `Simple` with
`!a.positional`

Added `mainargs.IssueTests.issue60` as a regression test, that fails on
main and passes on this PR. Existing tests all pass
@lefou lefou added this to the 0.5.0 milestone Apr 29, 2023
lihaoyi added a commit to com-lihaoyi/mill that referenced this pull request May 1, 2023
Fixes #1948. Should probably
be reviewed concurrently with
com-lihaoyi/mainargs#62 which it depends on

# Major Changes

1. Pull in com-lihaoyi/mainargs#62, which allows
MainArg's "aggregate leftover arguments" logic to apply to custom types
and not be hardcoded to `mainargs.Leftover`

2. Update MainArgs integration code in this repo to accomadate the
changes

3. Replace the `def run(args: String*)` with `def run(args: Task[Args] =
T.task(Args()))`.

# Minor Changes

1. I moved the `import mill.main.TokenReaders._` into the `Discover`
macro, so we can stop manually importing it everywhere.

2. I moved all the aliases from `import mill.define._` to `import
mill._`. `Task[_]` in particular is a type I believe we should start
encouraging people to use more often - e.g. annotating all command
parameter types as `Task[_]`s - and so it should be in the default
`import mill._` without needing a separate import.

3. Added a direct dependency to `com.lihaoyi::pprint`, since the
implicit dependency in `mainargs` was removed in 0.5.0 due to being
unnecessary

# Testing

1. All existing tests pass, at least those I've run locally so far
(`./mill -i -j 3 example.__.local.test`, can't use CI until the mainargs
PR is published). This includes unit tests that call `run`
programmatically and integration/example tests that call `run` via the
CLI.

4. Added a new example test `5-module-run-task`, that demonstrates and
explains how to use a `ScalaModule`'s `run` method to implement a
`task`, passing in input parameters from upstream tasks and using the
output (in this case generated files) in downstream tasks

# Notes

1. There is still some boilerplate around `run`, both defining it `args:
Task[Args] = T.task(Args())` and calling it programmatically,
`run(T.task(Args(...)))`. For now I haven't figured out how to reduce
this, as we already "used up" our implicit conversion budget with the
`T.*{...}` macros. Probably can be done, although it won't be quite as
low-boilerplate as the original `args: String*` syntax. Maybe in a
follow-up PR

2. The example test `5-module-run-task` is something I've wanted to be
able to write for years now. Nice to finally be able to do it without
having to piece together a `Jvm.runSubprocess()` call myself!

3. `5-module-run-task` is still a bit clunkier than I was hoping. Having
to `def barWorkingDir` beforehand to pass it to both the `bar.run` and
the `def sources` override is weird and unintuitive. Maybe we can change
the `def run` return type to make it easier to return the `T.dest`
folder or other metadata out of the body of `def run` so it can be used
by downstream tasks? e.g. making `def run` return `Command[PathRef]`
rather than `Command[Unit]`


5. IMO we should recommend that all CLI arguments to `T.command`s come
in the form of `Task[T]`s, unless there's concrete reasons otherwise
(e.g. we want to dynamically change `T.command` implementations based on
the input). That would maximise the ability to re-use commands in the
body of of tasks, which is something people probably want

---------

Co-authored-by: Tobias Roeser <le.petit.fou@web.de>
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.

None yet

2 participants