Skip to content

Commit 44b4da9

Browse files
authored
Allow to disable auto-setup-ide in build commands with a command line option & config (#4258)
1 parent 03ab36f commit 44b4da9

12 files changed

Lines changed: 213 additions & 11 deletions

File tree

modules/cli/src/main/scala/scala/cli/commands/setupide/SetupIde.scala

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,19 @@ object SetupIde extends ScalaCommand[SetupIdeOptions] {
9393
previousCommandName: Option[String],
9494
args: Seq[String]
9595
): Unit =
96-
writeBspConfiguration(
97-
SetupIdeOptions(shared = options),
98-
inputs,
99-
buildOptions,
100-
previousCommandName,
101-
args
102-
) match {
103-
case Left(ex) =>
104-
logger.debug(s"Ignoring error during setup-ide: ${ex.message}")
105-
case Right(_) =>
106-
}
96+
if options.autoSetupIdeEnabled then
97+
writeBspConfiguration(
98+
SetupIdeOptions(shared = options),
99+
inputs,
100+
buildOptions,
101+
previousCommandName,
102+
args
103+
) match {
104+
case Left(ex) =>
105+
logger.debug(s"Ignoring error during setup-ide: ${ex.message}")
106+
case Right(_) =>
107+
}
108+
else logger.debug("Auto setup-ide is disabled, skipping .bsp generation.")
107109

108110
override def sharedOptions(options: SetupIdeOptions): Option[SharedOptions] = Some(options.shared)
109111

modules/cli/src/main/scala/scala/cli/commands/shared/SharedOptions.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ final case class SharedOptions(
210210
@HelpMessage("Force object wrapper for scripts")
211211
@Tag(tags.experimental)
212212
objectWrapper: Option[Boolean] = None,
213+
@Group(HelpGroup.Scala.toString)
214+
@HelpMessage(
215+
"Automatically generate BSP configuration in `.bsp/` when running build commands. Enabled by default."
216+
)
217+
@Name("auto-setup-bsp")
218+
@Tag(tags.implementation)
219+
autoSetupIde: Option[Boolean] = None,
213220
@Recurse
214221
scope: ScopeOptions = ScopeOptions(),
215222

@@ -231,6 +238,14 @@ final case class SharedOptions(
231238
def logger: Logger = logging.logger
232239
override def global: GlobalOptions =
233240
GlobalOptions(logging = logging, globalSuppress = suppress.global, powerOptions = powerOptions)
241+
def autoSetupIdeEnabled: Boolean =
242+
autoSetupIde
243+
.orElse(
244+
ConfigDbUtils.getLatestConfigDbOpt(logger)
245+
.flatMap(_.get(Keys.autoSetupIde).toOption)
246+
.flatten
247+
)
248+
.getOrElse(true)
234249

235250
private def scalaJsOptions(opts: ScalaJsOptions): options.ScalaJsOptions = {
236251
import opts._

modules/config/src/main/scala/scala/cli/config/Keys.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ object Keys {
5454
specificationLevel = SpecificationLevel.IMPLEMENTATION,
5555
description = "Globally enables actionable diagnostics. Enabled by default."
5656
)
57+
val autoSetupIde = new Key.BooleanEntry(
58+
prefix = Seq("ide"),
59+
name = "auto-setup",
60+
specificationLevel = SpecificationLevel.IMPLEMENTATION,
61+
description =
62+
"Globally controls whether the BSP configuration in `.bsp/` is generated automatically by build commands. Enabled by default."
63+
)
5764
val interactive = new Key.BooleanEntry(
5865
prefix = Seq.empty,
5966
name = "interactive",
@@ -184,6 +191,7 @@ object Keys {
184191

185192
def all: Seq[Key[?]] = Seq[Key[?]](
186193
actions,
194+
autoSetupIde,
187195
defaultRepositories,
188196
deprecatedTestKey,
189197
ghToken,

modules/integration/src/test/scala/scala/cli/integration/CompileTestDefinitions.scala

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,34 @@ abstract class CompileTestDefinitions
212212
}
213213
}
214214

215+
test("compile auto setup-ide enabled by default") {
216+
TestInputs(
217+
os.rel / "Main.scala" ->
218+
"""object Main {
219+
| def main(args: Array[String]): Unit = println("Hello")
220+
|}
221+
|""".stripMargin
222+
).fromRoot { root =>
223+
val bspEntry = root / ".bsp" / "scala-cli.json"
224+
os.proc(TestUtil.cli, "compile", extraOptions, ".").call(cwd = root)
225+
assert(os.exists(bspEntry))
226+
}
227+
}
228+
229+
test("compile can disable auto setup-ide via --auto-setup-ide=false") {
230+
TestInputs(
231+
os.rel / "Main.scala" ->
232+
"""object Main {
233+
| def main(args: Array[String]): Unit = println("Hello")
234+
|}
235+
|""".stripMargin
236+
).fromRoot { root =>
237+
val bspEntry = root / ".bsp" / "scala-cli.json"
238+
os.proc(TestUtil.cli, "compile", extraOptions, ".", "--auto-setup-ide=false").call(cwd = root)
239+
assert(!os.exists(bspEntry))
240+
}
241+
}
242+
215243
test("exit code") {
216244
val inputs = TestInputs(
217245
os.rel / "Main.scala" ->

modules/integration/src/test/scala/scala/cli/integration/ConfigTests.scala

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,31 @@ class ConfigTests extends ScalaCliSuite {
4646
}
4747
}
4848

49+
test("ide.auto-setup controls implicit bsp generation for build commands") {
50+
val configFile = os.rel / "config" / "config.json"
51+
val configEnv = Map("SCALA_CLI_CONFIG" -> configFile.toString)
52+
TestInputs(
53+
os.rel / "Main.scala" ->
54+
"""object Main {
55+
| def main(args: Array[String]): Unit = println("Hello")
56+
|}
57+
|""".stripMargin
58+
).fromRoot { root =>
59+
val bspEntry = root / ".bsp" / "scala-cli.json"
60+
61+
os.proc(TestUtil.cli, "config", "ide.auto-setup", "false").call(cwd = root, env = configEnv)
62+
63+
os.proc(TestUtil.cli, "compile", ".").call(cwd = root, env = configEnv)
64+
assert(!os.exists(bspEntry))
65+
66+
os.proc(TestUtil.cli, "compile", "--auto-setup-ide=true", ".").call(
67+
cwd = root,
68+
env = configEnv
69+
)
70+
assert(os.exists(bspEntry))
71+
}
72+
}
73+
4974
test("password") {
5075
val configFile = os.rel / "config" / "config.json"
5176
val configEnv = Map("SCALA_CLI_CONFIG" -> configFile.toString)

modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,38 @@ abstract class RunTestDefinitions
123123
}
124124
}
125125

126+
test("run auto setup-ide enabled by default") {
127+
TestInputs(
128+
os.rel / "Main.scala" ->
129+
"""object Main {
130+
| def main(args: Array[String]): Unit = println("Hello from run")
131+
|}
132+
|""".stripMargin
133+
).fromRoot { root =>
134+
val bspEntry = root / ".bsp" / "scala-cli.json"
135+
val res = os.proc(TestUtil.cli, "run", extraOptions, ".").call(cwd = root)
136+
expect(res.out.trim() == "Hello from run")
137+
assert(os.exists(bspEntry))
138+
}
139+
}
140+
141+
test("run can disable auto setup-ide via --auto-setup-ide=false") {
142+
TestInputs(
143+
os.rel / "Main.scala" ->
144+
"""object Main {
145+
| def main(args: Array[String]): Unit = println("Hello from run")
146+
|}
147+
|""".stripMargin
148+
).fromRoot { root =>
149+
val bspEntry = root / ".bsp" / "scala-cli.json"
150+
val res = os.proc(TestUtil.cli, "run", extraOptions, ".", "--auto-setup-ide=false").call(cwd =
151+
root
152+
)
153+
expect(res.out.trim() == "Hello from run")
154+
assert(!os.exists(bspEntry))
155+
}
156+
}
157+
126158
test("Debugging") {
127159
val inputs = TestInputs(
128160
os.rel / "Foo.scala" ->

modules/integration/src/test/scala/scala/cli/integration/TestTestDefinitions.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,27 @@ abstract class TestTestDefinitions extends ScalaCliSuite with TestScalaVersionAr
234234
}
235235
}
236236

237+
test("test auto setup-ide enabled by default") {
238+
successfulTestInputs().fromRoot { root =>
239+
val bspEntry = root / ".bsp" / "scala-cli.json"
240+
val output = os.proc(TestUtil.cli, "test", extraOptions, ".").call(cwd = root).out.text()
241+
expect(output.contains("Hello from tests"))
242+
assert(os.exists(bspEntry))
243+
}
244+
}
245+
246+
test("test can disable auto setup-ide via --auto-setup-ide=false") {
247+
successfulTestInputs().fromRoot { root =>
248+
val bspEntry = root / ".bsp" / "scala-cli.json"
249+
val output =
250+
os.proc(TestUtil.cli, "test", extraOptions, ".", "--auto-setup-ide=false").call(cwd = root)
251+
.out
252+
.text()
253+
expect(output.contains("Hello from tests"))
254+
assert(!os.exists(bspEntry))
255+
}
256+
}
257+
237258
if (!Properties.isMac || !TestUtil.isCI)
238259
test("--watching with --watch re-runs tests on external file change") {
239260
val sourceFile = os.rel / "MyTests.test.scala"

website/docs/reference/cli-options.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,6 +1713,12 @@ Exclude sources
17131713

17141714
Force object wrapper for scripts
17151715

1716+
### `--auto-setup-ide`
1717+
1718+
Aliases: `--auto-setup-bsp`
1719+
1720+
Automatically generate BSP configuration in `.bsp/` when running build commands. Enabled by default.
1721+
17161722
### [deprecated] `--deprecated-test-option`
17171723

17181724
**Deprecated**: For testing purposes only.

website/docs/reference/commands.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Available keys:
5353
- httpProxy.address HTTP proxy address.
5454
- httpProxy.password HTTP proxy password (used for authentication).
5555
- httpProxy.user HTTP proxy user (used for authentication).
56+
- ide.auto-setup Globally controls whether the BSP configuration in `.bsp/` is generated automatically by build commands. Enabled by default.
5657
- interactive Globally enables interactive mode (the '--interactive' flag).
5758
- interactive-was-suggested Setting indicating if the global interactive mode was already suggested.
5859
- java.properties Java properties for Scala CLI's execution.

website/docs/reference/scala-command/cli-options.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,14 @@ Add toolkit to classPath (not supported in Scala 2.12), 'default' version for Sc
11641164

11651165
Exclude sources
11661166

1167+
### `--auto-setup-ide`
1168+
1169+
Aliases: `--auto-setup-bsp`
1170+
1171+
`IMPLEMENTATION specific` per Scala Runner specification
1172+
1173+
Automatically generate BSP configuration in `.bsp/` when running build commands. Enabled by default.
1174+
11671175
### [deprecated] `--deprecated-test-option`
11681176

11691177
**Deprecated**: For testing purposes only.

0 commit comments

Comments
 (0)