Skip to content

Commit 78fb232

Browse files
committed
Fix Travis CI issue with GWT composed sourcemaps
GWT does not support SourceMapResolver::getRelativePath
1 parent 4bf54b2 commit 78fb232

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

src/com/google/javascript/jscomp/Compiler.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ public SourceFile loadSource(String filename) {
295295
private static class ResolvedSourceMap {
296296
public String originalPath;
297297
public String sourceMapPath;
298-
public SourceFile sourceFile;
298+
public String relativePath;
299299
}
300300

301301
/**
@@ -2891,15 +2891,15 @@ public OriginalMapping getSourceMapping(String sourceName, int lineNumber, int c
28912891
// First check to see if the original file was loaded from an input source map.
28922892
String sourceMapOriginalPath = sourceMap.getOriginalPath();
28932893
String resultOriginalPath = result.getOriginalFile();
2894-
SourceFile source = null;
2894+
final String relativePath;
28952895

28962896
// Resolving the paths to a source file is expensive, so check the cache first.
28972897
if (sourceMapOriginalPath.equals(resolvedSourceMap.originalPath)
28982898
&& resultOriginalPath.equals(resolvedSourceMap.sourceMapPath)) {
2899-
source = resolvedSourceMap.sourceFile;
2899+
relativePath = resolvedSourceMap.relativePath;
29002900
} else {
2901-
String relativePath = resolveSibling(sourceMapOriginalPath, resultOriginalPath);
2902-
source = getSourceFileByName(relativePath);
2901+
relativePath = resolveSibling(sourceMapOriginalPath, resultOriginalPath);
2902+
SourceFile source = getSourceFileByName(relativePath);
29032903
if (source == null && !isNullOrEmpty(resultOriginalPath)) {
29042904
source =
29052905
SourceMapResolver.getRelativePath(
@@ -2912,11 +2912,11 @@ public OriginalMapping getSourceMapping(String sourceName, int lineNumber, int c
29122912
// Cache this resolved source for the next caller.
29132913
resolvedSourceMap.originalPath = sourceMapOriginalPath;
29142914
resolvedSourceMap.sourceMapPath = resultOriginalPath;
2915-
resolvedSourceMap.sourceFile = source;
2915+
resolvedSourceMap.relativePath = relativePath;
29162916
}
29172917

29182918
return result.toBuilder()
2919-
.setOriginalFile(source.getOriginalPath())
2919+
.setOriginalFile(relativePath)
29202920
.setColumnPosition(result.getColumnPosition() - 1)
29212921
.build();
29222922
}

test/com/google/javascript/jscomp/SourceMapTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import com.google.common.base.Joiner;
2222
import com.google.common.collect.ImmutableList;
23+
import com.google.common.collect.ImmutableMap;
2324
import com.google.debugging.sourcemap.SourceMapConsumer;
2425
import com.google.debugging.sourcemap.SourceMapConsumerV3;
2526
import com.google.debugging.sourcemap.SourceMapTestCase;
@@ -38,6 +39,7 @@ public final class SourceMapTest extends SourceMapTestCase {
3839
public SourceMapTest() {}
3940

4041
private List<SourceMap.LocationMapping> mappings;
42+
private ImmutableMap.Builder<String, SourceMapInput> inputMaps;
4143

4244
@Test
4345
public void testPrefixReplacement1() throws IOException {
@@ -149,19 +151,49 @@ public void testLambdaReplacement() throws IOException {
149151
"}\n"));
150152
}
151153

154+
// This is taken from SourceMapJsLangTest. That test can't run under J2CL because it
155+
// uses a parameterized runner but we do want to test this basic behavior under J2CL.
156+
@Test
157+
public void testRepeatedCompilation() throws Exception {
158+
// Run compiler twice feeding sourcemaps from the first run as input to the second run.
159+
// This way we ensure that compiler works fine with its own sourcemaps and doesn't lose
160+
// important information.
161+
String fileContent = "function foo() {} alert(foo());";
162+
String fileName = "foo.js";
163+
RunResult firstCompilation = compile(fileContent, fileName);
164+
String newFileName = fileName + ".compiled";
165+
inputMaps.put(
166+
newFileName,
167+
new SourceMapInput(
168+
SourceFile.fromCode("sourcemap", firstCompilation.sourceMapFileContent)));
169+
170+
RunResult secondCompilation = compile(firstCompilation.generatedSource, newFileName);
171+
check(
172+
fileName,
173+
fileContent,
174+
secondCompilation.generatedSource,
175+
secondCompilation.sourceMapFileContent);
176+
}
177+
152178
@Override
153179
protected CompilerOptions getCompilerOptions() {
154180
CompilerOptions options = super.getCompilerOptions();
155181
if (mappings != null) {
156182
options.sourceMapLocationMappings = mappings;
157183
}
184+
185+
if (!this.inputMaps.build().isEmpty()) {
186+
options.setApplyInputSourceMaps(true);
187+
options.setInputSourceMaps(this.inputMaps.build());
188+
}
158189
return options;
159190
}
160191

161192
@Override
162193
@Before
163194
public void setUp() {
164195
super.setUp();
196+
this.inputMaps = ImmutableMap.builder();
165197
}
166198

167199
private void checkSourceMap2(

0 commit comments

Comments
 (0)