Skip to content

Conversation

eed3si9n
Copy link
Member

@eed3si9n eed3si9n commented Oct 4, 2019

Fixes #3183

This implements an input task lintBuild that checks for unused settings/tasks. Because most settings are on the intermediary to other settings/tasks, they are included into the linting by default. The notable exceptions are settings used exclusively by a command. To opt-out, you can either append it to Global / excludeLintKeys or set the rank to invisible.

On the other hand, many tasks are on the leaf (called by human), so task keys are excluded from linting by default. However there are notable tasks that trip up users, so they are opted-in using Global / includeLintKeys.

lintBuild is also implemented as a function that takes a State, and I'm calling that during reload now.

lintBuild

credits

The core of the idea is straight from the snippet proposed by @havocp in 2011 unused settings thread that identified 58 unused settings. This filters the candidates down to locally defines settings only (also an idea discussed in the thread).

Fixes sbt#3183

This implements an input task  lintBuild that checks for unused settings/tasks.
Because most settings are on the intermediary to other settings/tasks, they are included into the linting by default. The notable exceptions are settings used exclusively by a command. To opt-out, you can either append it to `Global / excludeLintKeys` or set the rank to invisible.

On the other hand, many tasks are on the leaf (called by human), so task keys are excluded from linting by default. However there are notable tasks that trip up users, so they are opted-in using `Global / includeLintKeys`.

// input task version of the lintBuild
def lintBuildTask: Def.Initialize[InputTask[Unit]] = Def.inputTask {
val _ = Def.spaceDelimited().parsed // not used yet
Copy link
Contributor

Choose a reason for hiding this comment

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

What are the plans in the future for the input of the task?

Copy link
Member Author

Choose a reason for hiding this comment

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

I was thinking options that could tune the filtering, for example to show:

  • unused settings introduced by plugins
  • unused tasks in build.sbt
  • unused settings and tasks introduced by plugins

@dwijnand
Copy link
Member

dwijnand commented Oct 4, 2019

Wouldn't it be better to opt-in to such linting? Either by opting into linting on load or by just running a task (or command).

@jvican
Copy link
Member

jvican commented Oct 4, 2019

Great work 👍 I like that such linting is enabled by default and it doesn't require an opt-in. IMO build users should not opt-in to get some checks that are most likely always on point: if their task/setting implementation isn't used anywhere, either they have dead code, sbt has a bug in its build linting logic or they messed it up. Hopefully the first and third possibilities are the most likely.

@dwijnand
Copy link
Member

dwijnand commented Oct 4, 2019

If there's high confidence then instead of warning (and going ignored, for instance in CI) let it fail the build load.

Address other review comments
@eed3si9n
Copy link
Member Author

eed3si9n commented Oct 4, 2019

To be honest, I am not quite sure what the signal/noise ratio would be until people start using them, so this PR keeps the option somewhat open. Initially there might be some adjustment period needed, if there's a popular plugin that uses settings from a command. (Exclusion doesn't introduce new type, so plugins should be able to exclude without adding dependencies on sbt 1.4.x).

Once the dust settles during Mx or RC, and if it's mostly good suggestions, then we can keep it on as part of reload. Otherwise, we can just let users run lintUnused task or let ppl opt-in globally.

I think it would also be interesting to have a mode for plugin authors to self-check if their plugin wiring is kosher. (Currently only keys from local files are checked)

@eed3si9n eed3si9n added this to the 1.4.0 milestone Oct 4, 2019
@eed3si9n eed3si9n merged commit e17c64d into sbt:develop Oct 30, 2019
@eed3si9n eed3si9n deleted the wip/lint branch October 30, 2019 15:36
Nirvikalpa108 pushed a commit to Nirvikalpa108/sbt that referenced this pull request Sep 12, 2021
Currently crossSbtVersions is incorrectly generating a warning that it
is an unused setting (see sbt#5153). This
PR fixes this by adding it to the list of excluded lint keys.

Fixes sbt#6571.
Nirvikalpa108 pushed a commit to Nirvikalpa108/sbt that referenced this pull request Oct 30, 2021
Currently crossSbtVersions is incorrectly generating a warning that it
is an unused setting (see sbt#5153). This
PR fixes this by adding it to the list of excluded lint keys.

Fixes sbt#6571.
Nirvikalpa108 pushed a commit to Nirvikalpa108/sbt that referenced this pull request Oct 30, 2021
Currently crossSbtVersions is incorrectly generating a warning that it
is an unused setting (see sbt#5153). This
PR fixes this by adding it to the list of excluded lint keys.

Fixes sbt#6571.
tdas pushed a commit to unitycatalog/unitycatalog that referenced this pull request Jul 3, 2024
**PR Checklist**

- [x] A description of the changes is added to the description of this
PR.
- [x] If there is a related issue, make sure it is linked to this PR.

**Description of changes**
Resolves #91.

> Whenever build/sbt test command is run, the warnings below are printed
to the console:
> [warn] there are 3 keys that are not used by any other settings/tasks:
>    [warn]  
>    [warn] * cli / Compile / logLevel
> [warn] +- /home/runner/work/unitycatalog/unitycatalog/build.sbt:206

This issue comes from the [key
linting](sbt/sbt#5153) added in sbt 1.4.0, which
warns keys are used by some tasks but not by others. In our case,
`Compile / logLevel` is used in `cli`, `client`, `server` but not in
`apiDocs`.


<!-- Please state what you've changed and how it might affect the users.
-->

Signed-off-by: Tai Le Manh <manhtai.lmt@gmail.com>
rtyley added a commit to rtyley/sbt that referenced this pull request May 29, 2025
sbt added the `sonaDeploymentName` key with sbt#8126, released with https://github.com/sbt/sbt/releases/tag/v1.11.0 - in use, this seems to be getting lint warnings (as introduced by sbt#5153):

```
[warn] there's a key that's not used by any other settings/tasks:
[warn]  
[warn] * scala-collection-plus / sonaDeploymentName
[warn]   +- /home/runner/work/scala-collection-plus/scala-collection-plus/build.sbt:3
[warn]  
[warn] note: a setting might still be used by a command; to exclude a key from this `lintUnused` check
[warn] either append it to `Global / excludeLintKeys` or call .withRank(KeyRanks.Invisible) on the key
```

https://github.com/rtyley/scala-collection-plus/actions/runs/15326291872/job/43121748535#step:6:19

...sbt#5153 does say that "notable exceptions are settings used exclusively by a command" - I _think_ that maybe `sonaDeploymentName` is only used by the commands `sonaRelease` & `sonaUpload`, so it's necessary to add it to `excludeLintKeys`?
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.

Warn on unused settings (or tasks) when they are marked secondary
4 participants