Skip to content

Commit 1eaf74f

Browse files
reid-spencerclaude
andcommitted
Fix multi-file prettify output directory structure
Include files were getting the full CWD-relative path baked into the output directory (e.g., out/a/b/c/restaurant/file.riddl instead of out/restaurant/file.riddl). Added mandatory inputDir parameter to PrettifyPass.Options and PrettifyState so toDestination() strips the input file's parent directory prefix from include paths. Made inputDir mandatory (no default) to prevent silent fallback to the broken behavior. All callers updated. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent b0b70a0 commit 1eaf74f

File tree

9 files changed

+31
-13
lines changed

9 files changed

+31
-13
lines changed

commands/shared/src/main/scala/com/ossuminc/riddl/commands/PrettifyCommand.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,16 @@ class PrettifyCommand(using pc: PlatformContext)
9797
.map(_.getFileName.toString).getOrElse("")
9898
val outDir = options.outputDir
9999
.map(_.toString).getOrElse("")
100+
val inDir = options.inputFile
101+
.flatMap(p => Option(p.getParent))
102+
.map(_.toString).getOrElse("")
100103
standardPasses ++ Seq(
101104
{ (input: PassInput, outputs: PassesOutput) =>
102105
PrettifyPass(input, outputs, PrettifyPass.Options(
103106
flatten = options.singleFile,
104107
topFile = topFile,
105-
outputDir = outDir
108+
outputDir = outDir,
109+
inputDir = inDir
106110
))
107111
}
108112
)

commands/shared/src/main/scala/com/ossuminc/riddl/commands/UnbastifyCommand.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class UnbastifyCommand(using pc: PlatformContext) extends Command[UnbastifyComma
111111
val passInput = PassInput(nebula)
112112
// BAST stores all content inline (includes are resolved), so
113113
// flatten output to produce a single self-contained .riddl file
114-
val prettifyOptions = PrettifyPass.Options(flatten = true)
114+
val prettifyOptions = PrettifyPass.Options(flatten = true, inputDir = "")
115115

116116
// Create the pass and run it using Pass.runThesePasses
117117
val passes: PassCreators = Seq(

passes/jvm-native/src/test/scala/com/ossuminc/riddl/passes/RiddlTest.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ class RiddlTest extends AbstractTestingBasis {
218218
case Right(root) =>
219219
val input = PassInput(root)
220220
val outputs = PassesOutput()
221-
val options = PrettifyPass.Options(flatten = false)
221+
val options = PrettifyPass.Options(flatten = false, inputDir = "language/input/includes")
222222
val result = Pass.runPass[PrettifyOutput](
223223
input, outputs,
224224
PrettifyPass(input, outputs, options)
@@ -244,7 +244,7 @@ class RiddlTest extends AbstractTestingBasis {
244244
case Right(root) =>
245245
val input = PassInput(root)
246246
val outputs = PassesOutput()
247-
val options = PrettifyPass.Options(flatten = true)
247+
val options = PrettifyPass.Options(flatten = true, inputDir = "")
248248
val result = Pass.runPass[PrettifyOutput](
249249
input, outputs,
250250
PrettifyPass(input, outputs, options)

passes/jvm/src/test/scala/com/ossuminc/riddl/passes/prettify/PrettifyPassTest.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ abstract class PrettifyPassTest extends RiddlFilesTestBase {
2828
def runPrettify(source: RiddlParserInput, run: String): String = {
2929
val passes = standardPasses ++ Seq(
3030
{ (input: PassInput, outputs: PassesOutput) =>
31-
val options = PrettifyPass.Options(flatten = true)
31+
val options = PrettifyPass.Options(flatten = true, inputDir = "")
3232
PrettifyPass(input, outputs, options)
3333
}
3434
)
@@ -94,7 +94,7 @@ abstract class PrettifyPassTest extends RiddlFilesTestBase {
9494

9595
"PrettifyOutput" must {
9696
"construct" in { _ =>
97-
val ps = PrettifyState(flatten = true)
97+
val ps = PrettifyState(flatten = true, inputDir = "")
9898
ps.numFiles must be(1)
9999
val po = PrettifyOutput(Root.empty, Messages.empty, ps)
100100
po.messages must be(empty)

passes/shared/src/main/scala/com/ossuminc/riddl/passes/Riddl.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ object Riddl {
143143
val result = Pass.runPass[PrettifyOutput](
144144
input,
145145
outputs,
146-
PrettifyPass(input, outputs, PrettifyPass.Options(flatten = true))
146+
PrettifyPass(input, outputs, PrettifyPass.Options(flatten = true, inputDir = ""))
147147
)
148148
result.state.filesAsString
149149
end toRiddlText

passes/shared/src/main/scala/com/ossuminc/riddl/passes/prettify/PrettifyPass.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ import scala.scalajs.js.annotation.JSExportTopLevel
2020
@JSExportTopLevel("PrettifyPass$")
2121
object PrettifyPass extends PassInfo[PrettifyPass.Options]:
2222
val name: String = "prettify"
23-
def creator(options: PrettifyPass.Options = PrettifyPass.Options())(using PlatformContext) =
23+
def creator(options: PrettifyPass.Options)(using PlatformContext) =
2424
(in: PassInput, out: PassesOutput) => PrettifyPass(in, out, options)
2525
end creator
2626

2727
case class Options(
2828
flatten: Boolean = false,
2929
topFile: String = "",
30-
outputDir: String = ""
30+
outputDir: String = "",
31+
inputDir: String
3132
) extends PassOptions
3233
end PrettifyPass
3334

passes/shared/src/main/scala/com/ossuminc/riddl/passes/prettify/PrettifyState.scala

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,22 @@ import com.ossuminc.riddl.utils.{PlatformContext, URL}
1212
import scala.collection.mutable
1313
import scala.scalajs.js.annotation.JSExport
1414

15-
case class PrettifyState(flatten: Boolean = false, topFile: String = "prettify-output.riddl", outDir: String = ".")(using PlatformContext):
15+
case class PrettifyState(flatten: Boolean = false, topFile: String = "prettify-output.riddl", outDir: String = ".", inputDir: String)(using PlatformContext):
1616

1717
// Strip leading/trailing '/' from outDir for URL basis field, which
1818
// already gets '/' separators in URL format (scheme://authority/basis/path)
1919
private val normalizedOutDir: String =
2020
val stripped = if outDir.startsWith("/") then outDir.drop(1) else outDir
2121
if stripped.endsWith("/") then stripped.dropRight(1) else stripped
2222

23+
// Prefix to strip from include paths so output files are relative
24+
// to the main input file's directory, not to CWD
25+
private val inputDirPrefix: String =
26+
if inputDir.isEmpty then ""
27+
else
28+
val normalized = inputDir.replace('\\', '/')
29+
if normalized.endsWith("/") then normalized else normalized + "/"
30+
2331
def filesAsString: String = {
2432
closeStack()
2533
files.map(fe => fe.toString).mkString("\n")
@@ -40,7 +48,11 @@ case class PrettifyState(flatten: Boolean = false, topFile: String = "prettify-o
4048
}
4149

4250
def toDestination(url: URL): URL = {
43-
URL(url.scheme, url.authority, normalizedOutDir, url.path)
51+
val relativePath =
52+
if inputDirPrefix.nonEmpty && url.path.startsWith(inputDirPrefix) then
53+
url.path.drop(inputDirPrefix.length)
54+
else url.path
55+
URL(url.scheme, url.authority, normalizedOutDir, relativePath)
4456
}
4557
def numFiles: Int = files.length
4658

passes/shared/src/main/scala/com/ossuminc/riddl/passes/prettify/PrettifyVisitor.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ class PrettifyVisitor(options: PrettifyPass.Options)(using PlatformContext) exte
2020
if options.topFile.nonEmpty then options.topFile
2121
else "prettify-output.riddl",
2222
if options.outputDir.nonEmpty then options.outputDir
23-
else "."
23+
else ".",
24+
options.inputDir
2425
)
2526

2627
def result: PrettifyState = state

riddlLib/shared/src/main/scala/com/ossuminc/riddl/RiddlLib.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ object RiddlLib extends RiddlLib:
531531
val passInput = PassInput(root)
532532
val passes = Seq(
533533
PrettifyPass.creator(
534-
PrettifyPass.Options(flatten = true)
534+
PrettifyPass.Options(flatten = true, inputDir = "")
535535
)
536536
)
537537
val result = Pass.runThesePasses(passInput, passes)

0 commit comments

Comments
 (0)