Skip to content

Commit 8650b17

Browse files
committed
refactoring: simplify the passing of tla modules, matching the interface of tlatools
1 parent 7d67d15 commit 8650b17

File tree

5 files changed

+47
-70
lines changed

5 files changed

+47
-70
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ jobs:
4040
uses: gradle/actions/setup-gradle@v5
4141

4242
- name: Build with Gradle Wrapper
43-
env:
44-
TLAPS_LIBRARY: ${{ github.workspace }}/tlapm/library
45-
TLA_COMMUNITY_MODULES: ${{ github.workspace }}/CommunityModules
46-
run: ./gradlew build
43+
run: ./gradlew build -DTLA-Library="${{ github.workspace }}/tlapm/library:${{ github.workspace }}/CommunityModules"
4744

4845
- name: Upload SpotBugs report
4946
if: always()

.github/workflows/semantic-preservation.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,7 @@ jobs:
5151
uses: gradle/actions/setup-gradle@v5
5252

5353
- name: Run semantic preservation tests
54-
env:
55-
TLA_COMMUNITY_MODULES: ${{ github.workspace }}/CommunityModules
56-
TLAPS_LIBRARY: ${{ github.workspace }}/tlapm/library
57-
APALACHE_HOME: ${{ github.workspace }}/apalache
58-
run: ./gradlew semanticPreservationTest -Dtlaplus.examples.path=${{ github.workspace }}/tlaplus-examples
54+
run: ./gradlew semanticPreservationTest -Dtlaplus.examples.path=${{ github.workspace }}/tlaplus-examples -DTLA-Library="${{ github.workspace }}/tlapm/library:${{ github.workspace }}/CommunityModules:${{ github.workspace }}/apalache/src/tla"
5955

6056
- name: Upload test results
6157
uses: actions/upload-artifact@v6

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,25 @@ It will print your reformatted spec in output. If the optional "OUTFILE" paramet
5858
output to that file.
5959
You can use the input file as the output file as well. Run with `-help` parameter for the help text.
6060

61+
### Module search paths
62+
63+
If your spec uses `EXTENDS` to reference modules that are not in the same directory (e.g., TLAPS, Community Modules,
64+
or custom libraries), you can pass their locations via the `-DTLA-Library` JVM system property:
65+
66+
```
67+
java -DTLA-Library=/path/to/tlaps/library:/path/to/CommunityModules -jar tlaplus-formatter.jar MySpec.tla
68+
```
69+
70+
Multiple paths are separated by `:` on Linux/macOS or `;` on Windows. Standard modules (Naturals, Sequences,
71+
FiniteSets, TLC, etc.) are bundled with the formatter and do not need to be specified.
72+
6173
## VSCode integration
6274

63-
It's a work in progress, see [PR-327](https://github.com/tlaplus/vscode-tlaplus/pull/327/files) on vscode-tlaplus repo.
75+
The formatter is integrated into the [TLA+ VSCode extension](https://github.com/tlaplus/vscode-tlaplus). Use the
76+
standard "Format Document" command (`Shift+Alt+F`) to format TLA+ files. The extension automatically passes your
77+
configured `tlaplus.moduleSearchPaths` to the formatter.
78+
79+
You can disable the formatter via the `tlaplus.formatter.enabled` setting.
6480

6581
## Limitations
6682

build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ tasks.withType<Copy> {
6363
tasks.test {
6464
useJUnitPlatform()
6565
finalizedBy(tasks.jacocoTestReport)
66+
System.getProperty("TLA-Library")?.let { systemProperty("TLA-Library", it) }
6667
}
6768

6869
tasks.jacocoTestReport {
@@ -80,6 +81,7 @@ tasks.register<Test>("semanticPreservationTest") {
8081
}
8182
systemProperties(System.getProperties().filter { (k, _) -> k.toString().startsWith("tlaplus.") }
8283
.map { (k, v) -> k.toString() to v.toString() }.toMap())
84+
System.getProperty("TLA-Library")?.let { systemProperty("TLA-Library", it) }
8385
}
8486

8587
tasks.register("generateVersionProperties") {

src/main/java/me/fponzi/tlaplusformatter/SANYWrapper.java

Lines changed: 26 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ public static TreeNode load(File file) throws IOException, SanyFrontendException
3030

3131
String parentDirPath = file.getAbsoluteFile().getParent();
3232

33-
// Resolver for filenames, patched for wired modules.
34-
var filenameResolver = new CustomFilenameToStream(parentDirPath);
33+
// Build library paths: parent dir + any paths from -DTLA-Library
34+
var libraryPaths = buildLibraryPaths(parentDirPath);
35+
var filenameResolver = new CustomFilenameToStream(libraryPaths);
3536

3637
// Set a unique tmpdir to avoid race-condition in SANY
3738
// TODO: RM once https://github.com/tlaplus/tlaplus/issues/688 is fixed
@@ -43,6 +44,27 @@ public static TreeNode load(File file) throws IOException, SanyFrontendException
4344
return parseUnitContext.get(specObj.getRootModule().getName().toString()).getParseTree();
4445
}
4546

47+
/**
48+
* Builds the array of library paths for SANY's module resolution.
49+
* Includes the parent directory of the spec file and any paths
50+
* specified via the -DTLA-Library system property.
51+
*/
52+
static String[] buildLibraryPaths(String parentDirPath) {
53+
List<String> paths = new ArrayList<>();
54+
paths.add(parentDirPath);
55+
56+
String tlaLibrary = System.getProperty(SimpleFilenameToStream.TLA_LIBRARY);
57+
if (tlaLibrary != null && !tlaLibrary.isEmpty()) {
58+
for (String p : tlaLibrary.split(File.pathSeparator)) {
59+
if (!p.isEmpty()) {
60+
paths.add(p);
61+
}
62+
}
63+
}
64+
65+
return paths.toArray(new String[0]);
66+
}
67+
4668
public static void loadSpecObject(SpecObj specObj, File file, StringWriter errBuf) throws IOException, SanyFrontendException {
4769
var outStream = WriterOutputStream
4870
.builder()
@@ -88,65 +110,9 @@ private static void ThrowOnError(SpecObj specObj) {
88110
}
89111

90112
private static class CustomFilenameToStream extends SimpleFilenameToStream {
91-
private final List<String> additionalPaths;
92-
93-
public CustomFilenameToStream(String parentDirPath) {
94-
super(parentDirPath);
95-
this.additionalPaths = getAdditionalModulePaths();
113+
public CustomFilenameToStream(String[] libraryPaths) {
114+
super(libraryPaths);
96115
this.tmpDir.toFile().deleteOnExit();
97116
}
98-
99-
private static List<String> getAdditionalModulePaths() {
100-
List<String> paths = new ArrayList<>();
101-
// Check for community modules, TLAPS, and Apalache in common locations
102-
String[] possiblePaths = {
103-
// Community modules
104-
System.getenv("TLA_COMMUNITY_MODULES"),
105-
"/tmp/CommunityModules/modules",
106-
"/tmp/CommunityModules",
107-
System.getProperty("user.home") + "/.tlaplus/CommunityModules/modules",
108-
// TLAPS library
109-
System.getenv("TLAPS_LIBRARY"),
110-
"/tmp/tlapm/library",
111-
System.getProperty("user.home") + "/.tlaplus/tlaps/library",
112-
"/usr/local/lib/tlaps/library",
113-
// Apalache modules
114-
System.getenv("APALACHE_HOME") != null ? System.getenv("APALACHE_HOME") + "/src/tla" : null,
115-
"/tmp/apalache/src/tla",
116-
System.getProperty("user.home") + "/.tlaplus/apalache/src/tla"
117-
};
118-
for (String path : possiblePaths) {
119-
if (path != null) {
120-
File dir = new File(path);
121-
if (dir.exists() && dir.isDirectory()) {
122-
paths.add(path);
123-
}
124-
}
125-
}
126-
return paths;
127-
}
128-
129-
@Override
130-
public TLAFile resolve(String name, boolean isModule) {
131-
// First try with the default resolver.
132-
TLAFile sourceFile = super.resolve(name, isModule);
133-
if (sourceFile != null && sourceFile.exists()) {
134-
return sourceFile;
135-
}
136-
137-
// Try additional paths (community modules, etc.)
138-
// name already includes .tla extension when isModule=true
139-
String filename = name.endsWith(".tla") ? name : name + ".tla";
140-
for (String path : additionalPaths) {
141-
File file = new File(path, filename);
142-
if (file.exists()) {
143-
return new TLAFile(file.getAbsolutePath(), this);
144-
}
145-
}
146-
147-
// Return the result from parent resolver (non-existent TLAFile, not null)
148-
// to preserve SANY's expected behavior
149-
return sourceFile;
150-
}
151117
}
152118
}

0 commit comments

Comments
 (0)