Skip to content

Commit 5bacfc6

Browse files
committed
Don't migrate directives with fix for single-file projects
1 parent 94a6e37 commit 5bacfc6

File tree

4 files changed

+67
-31
lines changed

4 files changed

+67
-31
lines changed

modules/cli/src/main/scala/scala/cli/commands/fix/BuiltInRules.scala

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,26 @@ object BuiltInRules extends CommandHelpers {
3939
.left.map(CompositeBuildException(_))
4040
.orExit(logger)
4141

42+
val sourcesCount =
43+
mainSources.paths.length + mainSources.inMemory.length +
44+
testSources.paths.length + testSources.inMemory.length
45+
sourcesCount match
46+
case 0 =>
47+
logger.message("No sources to migrate directives from.")
48+
logger.message("Nothing to do.")
49+
case 1 =>
50+
logger.message("No need to migrate directives for a single source file project.")
51+
logger.message("Nothing to do.")
52+
case _ => migrateDirectives(inputs, buildOptions, mainSources, testSources, logger)
53+
}
54+
55+
private def migrateDirectives(
56+
inputs: Inputs,
57+
buildOptions: BuildOptions,
58+
mainSources: Sources,
59+
testSources: Sources,
60+
logger: Logger
61+
)(using ScalaCliInvokeData): Unit = {
4262
// Only initial inputs are used, new inputs discovered during processing of
4363
// CrossSources.forInput may be shared between projects
4464
val writableInputs: Seq[OnDisk] = inputs.flattened()
@@ -120,8 +140,8 @@ object BuiltInRules extends CommandHelpers {
120140
directivesFromWritableTestInputs
121141
.filterNot(ttd => isProjectFile(ttd.positions))
122142
.foreach(ttd => removeDirectivesFrom(ttd.positions, toKeep = ttd.noTestPrefixAvailable))
123-
124143
}
144+
125145
private def getProjectSources(inputs: Inputs, logger: Logger)(using
126146
ScalaCliInvokeData
127147
): Either[::[BuildException], (Sources, Sources)] = {

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,4 +447,38 @@ trait FixBuiltInRulesTestDefinitions { _: FixTestDefinitions =>
447447
expect(r.out.trim() == expectedMessage)
448448
}
449449
}
450+
451+
{
452+
val directive = "//> using dep com.lihaoyi::os-lib:0.11.3"
453+
for {
454+
(inputFileName, code) <- Seq(
455+
"raw.scala" -> s"""$directive
456+
|object Main extends App {
457+
| println(os.pwd)
458+
|}
459+
|""".stripMargin,
460+
"script.sc" -> s"""$directive
461+
|println(os.pwd)
462+
|""".stripMargin
463+
)
464+
if !Properties.isWin // TODO: make this run on Windows CI
465+
testInputs = TestInputs(os.rel / inputFileName -> code)
466+
}
467+
test(
468+
s"dont extract directives into project.scala for a single-file project: $inputFileName"
469+
) {
470+
testInputs.fromRoot { root =>
471+
val fixResult = os.proc(TestUtil.cli, "--power", "fix", ".", extraOptions)
472+
.call(cwd = root, stderr = os.Pipe)
473+
expect(fixResult.err.trim().contains(
474+
"No need to migrate directives for a single source file project"
475+
))
476+
expect(!os.exists(root / projectFileName))
477+
expect(os.read(root / inputFileName) == code)
478+
val runResult = os.proc(TestUtil.cli, "run", ".", extraOptions)
479+
.call(cwd = root, stderr = os.Pipe)
480+
expect(runResult.out.trim() == root.toString)
481+
}
482+
}
483+
}
450484
}

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

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ trait FixScalafixRulesTestDefinitions {
7878
os.rel / "Hello.scala" -> unusedValueInputsContent
7979
)
8080
val expectedContent: String = noCrLf {
81-
s"""package foo
81+
s"""//> using options $scalafixUnusedRuleOption
82+
|package foo
8283
|
8384
|object Hello {
8485
| def main(args: Array[String]): Unit = {
@@ -88,19 +89,11 @@ trait FixScalafixRulesTestDefinitions {
8889
|}
8990
|""".stripMargin
9091
}
91-
val expectedProjectContent: String = noCrLf {
92-
s"""// Main
93-
|//> using options $scalafixUnusedRuleOption
94-
|
95-
|""".stripMargin
96-
}
9792

9893
semanticRuleInputs.fromRoot { root =>
9994
os.proc(TestUtil.cli, "fix", "--power", ".", scalaVersionArgs).call(cwd = root)
10095
val updatedContent = noCrLf(os.read(root / "Hello.scala"))
10196
expect(updatedContent == expectedContent)
102-
val projectContent = noCrLf(os.read(root / "project.scala"))
103-
expect(projectContent == expectedProjectContent)
10497
}
10598
}
10699

@@ -136,9 +129,9 @@ trait FixScalafixRulesTestDefinitions {
136129
scalaVersionArgs
137130
).call(cwd = root)
138131
val updatedContent = noCrLf(os.read(root / "Hello.scala"))
139-
val projectContent = noCrLf(os.read(root / "project.scala"))
140132
val expected = noCrLf {
141-
s"""|package hello
133+
s"""|//> using options $scalafixUnusedRuleOption
134+
|package hello
142135
|
143136
|object Hello {
144137
| def a = {
@@ -148,15 +141,8 @@ trait FixScalafixRulesTestDefinitions {
148141
|}
149142
|""".stripMargin
150143
}
151-
val expectedProjectContent: String = noCrLf {
152-
s"""// Main
153-
|//> using options $scalafixUnusedRuleOption
154-
|
155-
|""".stripMargin
156-
}
157144

158145
expect(updatedContent == expected)
159-
expect(projectContent == expectedProjectContent)
160146

161147
}
162148
}
@@ -259,24 +245,18 @@ trait FixScalafixRulesTestDefinitions {
259245
os.rel / "Hello.scala" -> original
260246
)
261247
val expectedContent: String = noCrLf {
262-
"""|object CollectHeadOptionTest {
263-
| def x1: Option[String] = List(1, 2, 3).collectFirst{ case n if n % 2 == 0 => n.toString }
264-
|}
265-
|""".stripMargin
266-
}
267-
val expectedProjectContent: String = noCrLf {
268-
s"""// Main
269-
|$directive
270-
|
271-
|""".stripMargin
248+
s"""|$directive
249+
|
250+
|object CollectHeadOptionTest {
251+
| def x1: Option[String] = List(1, 2, 3).collectFirst{ case n if n % 2 == 0 => n.toString }
252+
|}
253+
|""".stripMargin
272254
}
273255

274256
inputs.fromRoot { root =>
275257
os.proc(TestUtil.cli, "fix", ".", "--power", scalaVersionArgs).call(cwd = root)
276258
val updatedContent = noCrLf(os.read(root / "Hello.scala"))
277259
expect(updatedContent == expectedContent)
278-
val projectContent = noCrLf(os.read(root / "project.scala"))
279-
expect(projectContent == expectedProjectContent)
280260
}
281261
}
282262

website/docs/commands/fix.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Files containing (experimental) `using target` directives, e.g. `//> using targe
3030
The original scope (`main` or `test`) of each extracted directive is respected. `main` scope directives are transformed
3131
them into their `test.*` equivalent when needed.
3232

33+
Note: directives won't be extracted for single-file projects.
34+
3335
## `scalafix` integration
3436

3537
Scala CLI is capable of running [Scalafix](https://scalacenter.github.io/scalafix/) (a refactoring and linting tool for Scala) on your project.

0 commit comments

Comments
 (0)