Follow up for trace pretty printing (#1227)

This commit is contained in:
Jen Basch
2025-10-08 09:36:42 -07:00
committed by GitHub
parent cf9d87373d
commit e230fcf1a9
29 changed files with 186 additions and 119 deletions

View File

@@ -23,6 +23,7 @@ import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.util.TempFile;
import org.openjdk.jmh.util.TempFileManager;
import org.pkl.core.evaluatorSettings.TraceMode;
import org.pkl.core.http.HttpClient;
import org.pkl.core.module.ModuleKeyFactories;
import org.pkl.core.repl.ReplRequest;
@@ -52,7 +53,7 @@ public class ListSort {
IoUtils.getCurrentWorkingDir(),
StackFrameTransformers.defaultTransformer,
false,
null);
TraceMode.COMPACT);
private static final List<Object> list = new ArrayList<>(100000);
static {

View File

@@ -15,6 +15,7 @@ import org.pkl.commons.test.FileTestUtils.rootProjectDir
import org.pkl.core.Loggers
import org.pkl.core.SecurityManagers
import org.pkl.core.StackFrameTransformers
import org.pkl.core.evaluatorSettings.TraceMode
import org.pkl.core.module.ModuleKeyFactories
import org.pkl.core.repl.ReplRequest
import org.pkl.core.repl.ReplResponse
@@ -97,7 +98,7 @@ class DocSnippetTestsEngine : HierarchicalTestEngine<DocSnippetTestsEngine.Execu
IoUtils.getCurrentWorkingDir(),
StackFrameTransformers.defaultTransformer,
false,
null
TraceMode.COMPACT,
)
return ExecutionContext(replServer)
}

View File

@@ -19,6 +19,7 @@ import org.pkl.cli.repl.Repl
import org.pkl.commons.cli.CliCommand
import org.pkl.core.Loggers
import org.pkl.core.SecurityManagers
import org.pkl.core.evaluatorSettings.TraceMode
import org.pkl.core.module.ModuleKeyFactories
import org.pkl.core.module.ModulePathResolver
import org.pkl.core.repl.ReplServer
@@ -68,7 +69,7 @@ internal class CliRepl(private val options: CliEvaluatorOptions) : CliCommand(op
options.base.normalizedWorkingDir,
stackFrameTransformer,
options.base.color?.hasColor() ?: false,
options.base.traceMode,
options.base.traceMode ?: TraceMode.COMPACT,
)
Repl(options.base.normalizedWorkingDir, server).run()
}

View File

@@ -21,6 +21,7 @@ import org.pkl.commons.toPath
import org.pkl.core.Loggers
import org.pkl.core.SecurityManagers
import org.pkl.core.StackFrameTransformers
import org.pkl.core.evaluatorSettings.TraceMode
import org.pkl.core.http.HttpClient
import org.pkl.core.module.ModuleKeyFactories
import org.pkl.core.repl.ReplRequest
@@ -43,7 +44,7 @@ class ReplMessagesTest {
"/".toPath(),
StackFrameTransformers.defaultTransformer,
false,
null,
TraceMode.COMPACT,
)
@Test

View File

@@ -295,16 +295,15 @@ class BaseOptions : OptionGroup() {
.multiple()
.toMap()
val traceMode: TraceMode by
val traceMode: TraceMode? by
option(
names = arrayOf("--trace-mode"),
metavar = "when",
metavar = "style",
help =
"Specifies how calls to trace() are formatted. Possible values of <when> are 'default', 'pretty' and 'hidden'.",
"Specifies how calls to trace() are formatted. Possible values of <style> are 'compact' and 'pretty'.",
)
.enum<TraceMode> { it.name.lowercase() }
.single()
.default(TraceMode.DEFAULT)
// hidden option used by native tests
private val testPort: Int by

View File

@@ -57,7 +57,7 @@ public class Analyzer {
@Nullable Path moduleCacheDir,
@Nullable DeclaredDependencies projectDependencies,
HttpClient httpClient,
@Nullable TraceMode traceMode) {
TraceMode traceMode) {
this.transformer = transformer;
this.color = color;
this.securityManager = securityManager;

View File

@@ -68,7 +68,7 @@ public final class EvaluatorBuilder {
private @Nullable DeclaredDependencies dependencies;
private @Nullable TraceMode traceMode;
private TraceMode traceMode = TraceMode.COMPACT;
private EvaluatorBuilder() {}

View File

@@ -82,7 +82,7 @@ public class EvaluatorImpl implements Evaluator {
@Nullable Path moduleCacheDir,
@Nullable DeclaredDependencies projectDependencies,
@Nullable String outputFormat,
@Nullable TraceMode traceMode) {
TraceMode traceMode) {
securityManager = manager;
frameTransformer = transformer;

View File

@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -80,6 +80,13 @@ public final class Loggers {
}
private static String formatMessage(String level, String message, StackFrame frame) {
return "pkl: " + level + ": " + message + " (" + frame.getModuleUri() + ')';
return "pkl: "
+ level
+ ": "
+ message
+ (message.endsWith("\n") ? "" : " ")
+ "("
+ frame.getModuleUri()
+ ')';
}
}

View File

@@ -18,8 +18,9 @@ package org.pkl.core.ast.expression.unary;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.source.SourceSection;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.pkl.core.ast.ExpressionNode;
import org.pkl.core.evaluatorSettings.TraceMode;
import org.pkl.core.runtime.*;
public final class TraceNode extends ExpressionNode {
@@ -27,9 +28,8 @@ public final class TraceNode extends ExpressionNode {
private static final int MAX_RENDERER_LENGTH = 1000000;
private final VmValueRenderer singleLineRenderer =
VmValueRenderer.singleLine(MAX_RENDERER_LENGTH);
private final VmValueRenderer multiLineRenderer = VmValueRenderer.multiLine(MAX_RENDERER_LENGTH);
private final VmValueRenderer compactRenderer = VmValueRenderer.singleLine(MAX_RENDERER_LENGTH);
private final VmValueRenderer prettyRenderer = VmValueRenderer.multiLine(Integer.MAX_VALUE);
public TraceNode(SourceSection sourceSection, ExpressionNode valueNode) {
super(sourceSection);
@@ -45,29 +45,23 @@ public final class TraceNode extends ExpressionNode {
@TruffleBoundary
private void doTrace(Object value, VmContext context) {
// If traces are disabled, returns early.
if (context.getTraceMode() == TraceMode.HIDDEN) {
return;
}
if (value instanceof VmObjectLike objectLike) {
try {
objectLike.force(true, true);
} catch (VmException ignored) {
}
}
VmValue.force(value, true);
var sourceSection = valueNode.getSourceSection();
String renderedValue;
if (context.getTraceMode() == TraceMode.PRETTY) {
renderedValue = multiLineRenderer.render(value);
} else {
renderedValue = singleLineRenderer.render(value);
}
var lhs = sourceSection.isAvailable() ? sourceSection.getCharacters().toString() : "<value>";
var message =
(sourceSection.isAvailable() ? sourceSection.getCharacters() : "<value")
+ " = "
+ renderedValue;
switch (context.getTraceMode()) {
case COMPACT -> lhs + " = " + compactRenderer.render(value);
case PRETTY -> {
var rhs = prettyRenderer.render(value);
yield (lhs.contains("\n") ? "\n" + addIndent(lhs, " ") + "\n=" : lhs + " =")
+ (rhs.contains("\n") ? "\n" + addIndent(rhs, " ") : " " + rhs)
+ "\n";
}
};
context.getLogger().trace(message, VmUtils.createStackFrame(sourceSection, null));
}
private static String addIndent(String s, String indent) {
return Arrays.stream(s.split("\n")).map((it) -> indent + it).collect(Collectors.joining("\n"));
}
}

View File

@@ -17,10 +17,8 @@ package org.pkl.core.evaluatorSettings;
/** Dictates the rendering of calls to the trace() method within Pkl. */
public enum TraceMode {
/** All trace() calls will not be emitted to stderr. */
HIDDEN,
/** All structures passed to trace() will be emitted on a single line. */
DEFAULT,
COMPACT,
/** All structures passed to trace() will be indented and emitted across multiple lines. */
PRETTY
}

View File

@@ -84,7 +84,7 @@ public class ReplServer implements AutoCloseable {
Path workingDir,
StackFrameTransformer frameTransformer,
boolean color,
@Nullable TraceMode traceMode) {
TraceMode traceMode) {
this.workingDir = workingDir;
this.securityManager = securityManager;

View File

@@ -22,6 +22,7 @@ import java.util.Map;
import org.pkl.core.Loggers;
import org.pkl.core.SecurityManagers;
import org.pkl.core.StackFrameTransformers;
import org.pkl.core.evaluatorSettings.TraceMode;
import org.pkl.core.http.HttpClient;
import org.pkl.core.module.ModuleKeyFactories;
import org.pkl.core.module.ModuleKeys;
@@ -50,7 +51,7 @@ public abstract class StdLibModule {
null,
null,
null,
null));
TraceMode.COMPACT));
var language = VmLanguage.get(null);
var moduleKey = ModuleKeys.standardLibrary(uri);
var source = VmUtils.loadSource((ResolvedModuleKey) moduleKey);

View File

@@ -66,7 +66,7 @@ public final class VmContext {
@Nullable String outputFormat,
@Nullable PackageResolver packageResolver,
@Nullable ProjectDependenciesManager projectDependenciesManager,
@Nullable TraceMode traceMode) {
TraceMode traceMode) {
this.frameTransformer = frameTransformer;
this.securityManager = securityManager;
@@ -148,7 +148,7 @@ public final class VmContext {
return holder.projectDependenciesManager;
}
public @Nullable TraceMode getTraceMode() {
public TraceMode getTraceMode() {
return holder.traceMode;
}
}

View File

@@ -1,5 +1,5 @@
amends "pkl:Project"
evaluatorSettings {
traceMode = "default"
traceMode = "compact"
}

View File

@@ -1,5 +0,0 @@
amends "pkl:Project"
evaluatorSettings {
traceMode = "hidden"
}

View File

@@ -1,18 +0,0 @@
amends ".../snippetTest.pkl"
examples {
["traceMode = 'hidden' results in no trace output"] {
trace(new {
["Parrot"] {
name = "Parrot"
age = 1234
bucketSettings = new {
kicked = true
["fjordConfig"] = new {
isPining = true
}
}
}
})
}
}

View File

@@ -1,7 +1,51 @@
amends ".../snippetTest.pkl"
examples {
["traceMode = 'pretty' results in indented, multi-line output"] {
["single-line lhs, single-line rhs"] {
let (val = new Dynamic {
["Parrot"] {
name = "Parrot"
age = 1234
bucketSettings = new {
kicked = true
["fjordConfig"] = new {
isPining = true
}
}
}
})
trace(val["Parrot"].name)
}
["single-line lhs, multi-line rhs"] {
let (val = new Dynamic {
["Parrot"] {
name = "Parrot"
age = 1234
bucketSettings = new {
kicked = true
["fjordConfig"] = new {
isPining = true
}
}
}
})
trace(val)
}
["multi-line lhs, single-line rhs"] {
trace((new Dynamic {
["Parrot"] {
name = "Parrot"
age = 1234
bucketSettings = new {
kicked = true
["fjordConfig"] = new {
isPining = true
}
}
}
})["Parrot"].name)
}
["multi-line lhs, multi-line rhs"] {
trace(new {
["Parrot"] {
name = "Parrot"

View File

@@ -25,4 +25,4 @@ pkl: TRACE: new {
}
}
}
} = new Dynamic { ["Parrot"] { name = "Parrot"; age = 1234; bucketSettings { kicked = true; ["fjordConfig"] { isPining = true } } } } (file:///$snippetsDir/input/projects/defaultTraceMode/defaultTraceMode.pkl)
} = new Dynamic { ["Parrot"] { name = "Parrot"; age = 1234; bucketSettings { kicked = true; ["fjordConfig"] { isPining = true } } } } (file:///$snippetsDir/input/projects/compactTraceMode/compactTraceMode.pkl)

View File

@@ -1,16 +0,0 @@
examples {
["traceMode = 'hidden' results in no trace output"] {
new {
["Parrot"] {
name = "Parrot"
age = 1234
bucketSettings {
kicked = true
["fjordConfig"] {
isPining = true
}
}
}
}
}
}

View File

@@ -1,5 +1,25 @@
examples {
["traceMode = 'pretty' results in indented, multi-line output"] {
["single-line lhs, single-line rhs"] {
"Parrot"
}
["single-line lhs, multi-line rhs"] {
new {
["Parrot"] {
name = "Parrot"
age = 1234
bucketSettings {
kicked = true
["fjordConfig"] {
isPining = true
}
}
}
}
}
["multi-line lhs, single-line rhs"] {
"Parrot"
}
["multi-line lhs, multi-line rhs"] {
new {
["Parrot"] {
name = "Parrot"
@@ -14,26 +34,61 @@ examples {
}
}
}
pkl: TRACE: new {
["Parrot"] {
name = "Parrot"
age = 1234
bucketSettings = new {
kicked = true
["fjordConfig"] = new {
isPining = true
}
pkl: TRACE: val["Parrot"].name = "Parrot"
(file:///$snippetsDir/input/projects/prettyTraceMode/prettyTraceMode.pkl)
pkl: TRACE: val =
new Dynamic {
["Parrot"] {
name = "Parrot"
age = 1234
bucketSettings {
kicked = true
["fjordConfig"] {
isPining = true
}
}
} = new Dynamic {
["Parrot"] {
name = "Parrot"
age = 1234
bucketSettings {
kicked = true
["fjordConfig"] {
isPining = true
}
}
}
} (file:///$snippetsDir/input/projects/prettyTraceMode/prettyTraceMode.pkl)
(file:///$snippetsDir/input/projects/prettyTraceMode/prettyTraceMode.pkl)
pkl: TRACE:
(new Dynamic {
["Parrot"] {
name = "Parrot"
age = 1234
bucketSettings = new {
kicked = true
["fjordConfig"] = new {
isPining = true
}
}
}
})["Parrot"].name
= "Parrot"
(file:///$snippetsDir/input/projects/prettyTraceMode/prettyTraceMode.pkl)
pkl: TRACE:
new {
["Parrot"] {
name = "Parrot"
age = 1234
bucketSettings = new {
kicked = true
["fjordConfig"] = new {
isPining = true
}
}
}
}
=
new Dynamic {
["Parrot"] {
name = "Parrot"
age = 1234
bucketSettings {
kicked = true
["fjordConfig"] {
isPining = true
}
}
}
}
(file:///$snippetsDir/input/projects/prettyTraceMode/prettyTraceMode.pkl)

View File

@@ -23,6 +23,7 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import org.pkl.commons.test.PackageServer
import org.pkl.commons.writeString
import org.pkl.core.evaluatorSettings.TraceMode
import org.pkl.core.http.HttpClient
import org.pkl.core.module.ModuleKeyFactories
import org.pkl.core.project.Project
@@ -37,7 +38,7 @@ class AnalyzerTest {
null,
null,
HttpClient.dummyClient(),
null,
TraceMode.COMPACT,
)
@Test
@@ -116,7 +117,7 @@ class AnalyzerTest {
tempDir.resolve("packages"),
null,
HttpClient.dummyClient(),
null,
TraceMode.COMPACT,
)
PackageServer.populateCacheDir(tempDir.resolve("packages"))
val file1 =
@@ -192,7 +193,7 @@ class AnalyzerTest {
tempDir.resolve("packages"),
project.dependencies,
HttpClient.dummyClient(),
null,
TraceMode.COMPACT,
)
val file1 =
tempDir
@@ -305,7 +306,7 @@ class AnalyzerTest {
tempDir.resolve("packages"),
project.dependencies,
HttpClient.dummyClient(),
null,
TraceMode.COMPACT,
)
val result = analyzer.importGraph(mainPkl.toUri())
val birdUri = URI("projectpackage://localhost:0/birds@1.0.0#/bird.pkl")

View File

@@ -18,6 +18,7 @@ package org.pkl.core
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.pkl.commons.toPath
import org.pkl.core.evaluatorSettings.TraceMode
import org.pkl.core.http.HttpClient
import org.pkl.core.module.ModuleKeyFactories
import org.pkl.core.repl.ReplRequest
@@ -45,7 +46,7 @@ class ReplServerTest {
"/".toPath(),
StackFrameTransformers.defaultTransformer,
false,
null,
TraceMode.COMPACT,
)
@Test

View File

@@ -40,7 +40,7 @@ internal class BinaryEvaluator(
moduleCacheDir: Path?,
declaredDependencies: DeclaredDependencies?,
outputFormat: String?,
traceMode: TraceMode?,
traceMode: TraceMode,
) :
EvaluatorImpl(
transformer,

View File

@@ -25,6 +25,7 @@ import java.util.regex.Pattern
import kotlin.random.Random
import org.pkl.core.*
import org.pkl.core.evaluatorSettings.PklEvaluatorSettings
import org.pkl.core.evaluatorSettings.TraceMode
import org.pkl.core.externalreader.ExternalReaderProcess
import org.pkl.core.externalreader.ExternalResourceResolver
import org.pkl.core.externalreader.ModuleReaderSpec
@@ -227,7 +228,7 @@ class Server(private val transport: MessageTransport) : AutoCloseable {
cacheDir,
dependencies,
message.outputFormat,
message.traceMode,
message.traceMode ?: TraceMode.COMPACT,
)
} catch (e: IllegalArgumentException) {
throw ProtocolException(e.message ?: "Failed to create an evalutor. $e", e)

View File

@@ -21,6 +21,7 @@ import kotlin.reflect.KClass
import org.junit.platform.commons.annotation.Testable
import org.pkl.commons.test.InputOutputTestEngine
import org.pkl.core.*
import org.pkl.core.evaluatorSettings.TraceMode
import org.pkl.core.http.HttpClient
import org.pkl.core.module.ModuleKeyFactories
@@ -56,7 +57,7 @@ class BinaryEvaluatorSnippetTestEngine : InputOutputTestEngine() {
null,
null,
null,
null,
TraceMode.COMPACT,
)
private fun String.stripFilePaths() =

View File

@@ -21,6 +21,7 @@ import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.pkl.core.*
import org.pkl.core.evaluatorSettings.TraceMode
import org.pkl.core.http.HttpClient
import org.pkl.core.module.ModuleKeyFactories
import org.pkl.core.resource.ResourceReaders
@@ -45,7 +46,7 @@ class BinaryEvaluatorTest {
null,
null,
null,
null,
TraceMode.COMPACT,
)
private fun evaluate(text: String, expression: String?) =

View File

@@ -113,11 +113,10 @@ externalResourceReaders: Mapping<String, ExternalReader>?
///
/// Possible values:
///
/// - `"hidden"`: All trace() calls will not be emitted to stderr.
/// - `"default"`: All structures passed to trace() will be emitted on a single line.
/// - `"compact"`: All structures passed to trace() will be emitted on a single line.
/// - `"pretty"`: All structures passed to trace() will be indented and emitted across multiple lines.
@Since { version = "0.30.0" }
traceMode: ("hidden"|"default"|"pretty")?
traceMode: ("compact"|"pretty")?
const local hostnameRegex = Regex(#"https?://([^/?#]*)"#)