Skip to content

Commit fc1aa20

Browse files
eregonandrykonchin
authored andcommitted
Avoid storing the source path in RubySource because that is problematic for pre-initialization
* Notably the absolute path of did_you_mean.rb, etc would get in the image and fail the check.
1 parent 1866c85 commit fc1aa20

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

src/main/java/org/truffleruby/parser/RubySource.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@
3030
public final class RubySource {
3131

3232
private final Source source;
33-
/** The path that will be used by the parser for __FILE__, warnings and syntax errors. Currently, the same as
34-
* {@link RubyLanguage#getPath(Source)}. Kept separate as we might want to change Source#getName() for non-file
35-
* Sources in the future (but then we'll need to still use this path in Ruby backtraces). */
36-
private final String sourcePath;
3733
private final TruffleString code;
3834
private byte[] bytes;
3935
private final RubyEncoding encoding;
@@ -52,11 +48,14 @@ private RubySource(Source source, String sourcePath, TStringWithEncoding code, b
5248
this(source, sourcePath, code, isEval, 0);
5349
}
5450

51+
/** @param sourcePath The path that will be used by the parser for __FILE__, warnings and syntax errors. Currently,
52+
* the same as {@link RubyLanguage#getPath(Source)}. Kept separate as we might want to change
53+
* Source#getName() for non-file Sources in the future (but then we'll need to still use this path in
54+
* Ruby backtraces). */
5555
public RubySource(Source source, String sourcePath, TStringWithEncoding code, boolean isEval, int lineOffset) {
5656
assert RubyLanguage.getPath(source).equals(sourcePath) : RubyLanguage.getPath(source) + " vs " + sourcePath;
57+
5758
this.source = Objects.requireNonNull(source);
58-
//intern() to improve footprint
59-
this.sourcePath = Objects.requireNonNull(sourcePath).intern();
6059

6160
if (code == null) {
6261
// We only have the Source, which only contains a java.lang.String.
@@ -87,8 +86,9 @@ public Source getSource() {
8786
return source;
8887
}
8988

90-
public String getSourcePath() {
91-
return sourcePath;
89+
public String getSourcePath(RubyLanguage language) {
90+
// NOTE: we avoid storing the source path in RubySource because that is problematic for pre-initialization
91+
return language.getSourcePath(source);
9292
}
9393

9494
public TruffleString getTruffleString() {

src/main/java/org/truffleruby/parser/TranslatorDriver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public RootCallTarget parse(RubySource rubySource, ParserContext parserContext,
157157
}
158158
}
159159

160-
boolean isInlineSource = rubySource.getSourcePath().equals("-e");
160+
boolean isInlineSource = rubySource.getSourcePath(language).equals("-e");
161161
boolean isEvalParse = parserContext.isEval();
162162
final ParserConfiguration parserConfiguration = new ParserConfiguration(
163163
context,

src/main/java/org/truffleruby/parser/YARPTranslatorDriver.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ public RootCallTarget parse(RubySource rubySource, ParserContext parserContext,
166166
}
167167
}
168168

169-
boolean isInlineSource = rubySource.getSourcePath().equals("-e");
169+
String sourcePath = rubySource.getSourcePath(language);
170+
boolean isInlineSource = sourcePath.equals("-e");
170171
boolean isEvalParse = parserContext.isEval();
171172
final ParserConfiguration parserConfiguration = new ParserConfiguration(
172173
context,
@@ -430,8 +431,11 @@ public static org.prism.ParseResult parseToYARPAST(RubyContext context, RubyLang
430431
byte version = (byte) 1;
431432
byte[][][] scopes;
432433

434+
// intern() to improve footprint
435+
String sourcePath = rubySource.getSourcePath(language).intern();
436+
433437
if (rubySource.isEval()) {
434-
filepath = rubySource.getSourcePath().getBytes(Encodings.FILESYSTEM_CHARSET);
438+
filepath = sourcePath.getBytes(Encodings.FILESYSTEM_CHARSET);
435439

436440
int scopesCount = localVariableNames.size();
437441
scopes = new byte[scopesCount][][];
@@ -452,7 +456,7 @@ public static org.prism.ParseResult parseToYARPAST(RubyContext context, RubyLang
452456
} else {
453457
assert localVariableNames.isEmpty(); // parsing of the whole source file cannot have outer scopes
454458

455-
filepath = rubySource.getSourcePath().getBytes(Encodings.FILESYSTEM_CHARSET);
459+
filepath = sourcePath.getBytes(Encodings.FILESYSTEM_CHARSET);
456460
scopes = new byte[0][][];
457461
}
458462

@@ -465,7 +469,6 @@ public static org.prism.ParseResult parseToYARPAST(RubyContext context, RubyLang
465469
ParseResult parseResult = YARPLoader.load(serializedBytes, yarpSource, context.getEncodingManager(),
466470
rubySource);
467471

468-
final String filename = rubySource.getSourcePath();
469472
final ParseResult.Error[] errors = parseResult.errors;
470473

471474
// collect warnings generated by the parser
@@ -475,8 +478,8 @@ public static org.prism.ParseResult parseToYARPAST(RubyContext context, RubyLang
475478
int lineNumber = RubySource.getStartLineAdjusted(context, section);
476479

477480
switch (warning.level) {
478-
case WARNING_DEFAULT -> rubyWarnings.warn(filename, lineNumber, warning.message);
479-
case WARNING_VERBOSE -> rubyWarnings.warning(filename, lineNumber, warning.message);
481+
case WARNING_DEFAULT -> rubyWarnings.warn(sourcePath, lineNumber, warning.message);
482+
case WARNING_VERBOSE -> rubyWarnings.warning(sourcePath, lineNumber, warning.message);
480483
}
481484
}
482485

src/main/java/org/truffleruby/parser/lexer/LexerSource.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
import com.oracle.truffle.api.strings.TruffleString;
4040
import org.jcodings.Encoding;
41+
import org.truffleruby.RubyLanguage;
4142
import org.truffleruby.core.encoding.RubyEncoding;
4243
import org.truffleruby.core.string.TStringConstants;
4344
import org.truffleruby.parser.RubySource;
@@ -59,7 +60,7 @@ public final class LexerSource {
5960

6061
public LexerSource(RubySource rubySource) {
6162
this.source = rubySource.getSource();
62-
this.sourcePath = rubySource.getSourcePath();
63+
this.sourcePath = RubyLanguage.getPath(rubySource.getSource());
6364

6465
final RubyEncoding rubyEncoding = rubySource.getEncoding();
6566
this.sourceBytes = rubySource.getTruffleString();

0 commit comments

Comments
 (0)