Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diagnostic path log relative to current working dir #4807

Merged
merged 7 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .chronus/changes/diag-path-relative-2024-9-21-16-52-22.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: feature
packages:
- "@typespec/compiler"
---

CLI logs diagnostic source path relative to the CWD.
timotheeguerin marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 2 additions & 1 deletion packages/compiler/src/core/cli/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@
}

export function createCLICompilerHost(options: CliHostArgs): CliCompilerHost {
const logSink = createConsoleSink({ pretty: options.pretty });
const logSink = createConsoleSink({ pretty: options.pretty, pathRelativeTo: process.cwd() });
const logger = createLogger({ sink: logSink, level: options.debug ? "trace" : "warning" });
console.log("Abc");

Check warning on line 59 in packages/compiler/src/core/cli/utils.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement
return { ...NodeHost, logSink, logger, debug: options.debug ?? false };
}

Expand Down
9 changes: 7 additions & 2 deletions packages/compiler/src/core/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ export function logDiagnostics(diagnostics: readonly Diagnostic[], logger: LogSi
}
}

export function formatDiagnostic(diagnostic: Diagnostic) {
export interface FormatDiagnosticOptions {
readonly pretty?: boolean;
readonly pathRelativeTo?: string;
}

export function formatDiagnostic(diagnostic: Diagnostic, options: FormatDiagnosticOptions = {}) {
return formatLog(
{
code: diagnostic.code,
Expand All @@ -50,7 +55,7 @@ export function formatDiagnostic(diagnostic: Diagnostic) {
url: diagnostic.url,
sourceLocation: getSourceLocation(diagnostic.target, { locateId: true }),
},
{ pretty: false },
{ pretty: options?.pretty ?? false, pathRelativeTo: options?.pathRelativeTo },
);
}

Expand Down
7 changes: 6 additions & 1 deletion packages/compiler/src/core/logger/console-sink.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { codeFrameColumns } from "@babel/code-frame";
import { relative } from "path/posix";
import pc from "picocolors";
import { Formatter } from "picocolors/types.js";
import { LogLevel, LogSink, ProcessedLog, SourceLocation } from "../types.js";
import { supportsHyperlink } from "./support-hyperlinks.js";

export interface FormatLogOptions {
pathRelativeTo?: string;
pretty?: boolean;
}

Expand Down Expand Up @@ -60,8 +62,11 @@ function formatLevel(options: FormatLogOptions, level: LogLevel) {

function formatSourceLocation(options: FormatLogOptions, location: SourceLocation) {
const postition = getLineAndColumn(location);
const path = color(options, location.file.path, pc.cyan);
const prePath = options.pathRelativeTo
? relative(process.cwd(), location.file.path)
: location.file.path;

const path = color(options, prePath, pc.cyan);
const line = color(options, postition.start.line.toString(), pc.yellow);
const column = color(options, postition.start.column.toString(), pc.yellow);
return `${path}:${line}:${column}`;
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/testing/expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function expectDiagnosticEmpty(diagnostics: readonly Diagnostic[]) {
}

function formatDiagnostics(diagnostics: readonly Diagnostic[]) {
return diagnostics.map(formatDiagnostic).join("\n");
return diagnostics.map((x) => formatDiagnostic(x)).join("\n");
}
/**
* Condition to match
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/test/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1423,7 +1423,7 @@ function parseEach(cases: (string | [string, Callback])[], options?: ParseOption

logVerboseTestOutput("\n=== Diagnostics ===");
if (astNode.parseDiagnostics.length > 0) {
const diagnostics = astNode.parseDiagnostics.map(formatDiagnostic).join("\n");
const diagnostics = astNode.parseDiagnostics.map((x) => formatDiagnostic(x)).join("\n");
assert.strictEqual(
hasParseError(astNode),
astNode.parseDiagnostics.some((e) => e.severity === "error"),
Expand Down
2 changes: 1 addition & 1 deletion packages/http-server-javascript/src/common/scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function createScalarsMap(program: Program): Map<Scalar, string> {

for (const [[type, diagnostics]] of entries) {
if (!type) {
const diagnosticString = diagnostics.map(formatDiagnostic).join("\n");
const diagnosticString = diagnostics.map((x) => formatDiagnostic(x)).join("\n");
throw new Error(`failed to construct TypeSpec -> JavaScript scalar map: ${diagnosticString}`);
} else if (type.kind !== "Scalar") {
throw new Error(
Expand Down
4 changes: 2 additions & 2 deletions packages/protobuf/src/transform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ function tspToProto(program: Program, emitterOptions: ProtobufEmitterOptions): P

if (!emptyType) {
throw new Error(
`Could not resolve the empty type: ${diagnostics.map(formatDiagnostic).join("\n")}`,
`Could not resolve the empty type: ${diagnostics.map((x) => formatDiagnostic(x)).join("\n")}`,
);
}

Expand Down Expand Up @@ -609,7 +609,7 @@ function tspToProto(program: Program, emitterOptions: ProtobufEmitterOptions): P

for (const [[type, diagnostics]] of entries) {
if (!type) {
const diagnosticString = diagnostics.map(formatDiagnostic).join("\n");
const diagnosticString = diagnostics.map((x) => formatDiagnostic(x)).join("\n");
throw new Error(
`Failed to construct TypeSpec -> Protobuf scalar map. Unexpected failure to resolve TypeSpec scalar: ${diagnosticString}`,
);
Expand Down
2 changes: 1 addition & 1 deletion packages/protobuf/test/scenarios.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ async function doEmit(
.filter(([name]) => name.startsWith(baseOutputPath))
.map(([name, value]) => [name.replace(baseOutputPath, ""), value]),
),
diagnostics: diagnostics.map(formatDiagnostic),
diagnostics: diagnostics.map((x) => formatDiagnostic(x)),
};
}

Expand Down
Loading