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

Disable workers with string literals and improve diagnostics #6536

Merged
merged 15 commits into from
Jul 6, 2021
Merged
2 changes: 0 additions & 2 deletions packages/core/core/src/Transformation.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,10 @@ export default class Transformation {
this.options.projectRoot,
asset.value.filePath,
)}`,
filePath: asset.value.filePath,
},
{
origin: '@parcel/core',
message: escapeMarkdown(err.message),
filePath: asset.value.filePath,
},
]);
}
Expand Down
88 changes: 47 additions & 41 deletions packages/core/core/src/loadParcelPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,24 @@ export default async function loadPlugin<T>(
resolveFrom,
)}. Either include it in "dependencies" or "parcelDependencies".`,
origin: '@parcel/core',
filePath: configPkg.files[0].filePath,
language: 'json5',
codeFrame:
codeFrames:
configPkg.config.dependencies ||
configPkg.config.parcelDependencies
? {
code: contents,
codeHighlights: generateJSONCodeHighlights(contents, [
{
key: configPkg.config.parcelDependencies
? '/parcelDependencies'
: '/dependencies',
type: 'key',
},
]),
}
? [
{
filePath: configPkg.files[0].filePath,
language: 'json5',
code: contents,
codeHighlights: generateJSONCodeHighlights(contents, [
{
key: configPkg.config.parcelDependencies
? '/parcelDependencies'
: '/dependencies',
type: 'key',
},
]),
},
]
: undefined,
},
});
Expand Down Expand Up @@ -111,23 +113,25 @@ export default async function loadPlugin<T>(
diagnostic: {
message: md`Cannot find Parcel plugin "${pluginName}"`,
origin: '@parcel/core',
filePath: configPath,
language: 'json5',
codeFrame: keyPath
? {
code: configContents,
codeHighlights: generateJSONCodeHighlights(configContents, [
{
key: keyPath,
type: 'value',
message: md`Cannot find module "${pluginName}"${
alternatives[0]
? `, did you mean "${alternatives[0]}"?`
: ''
}`,
},
]),
}
codeFrames: keyPath
? [
{
filePath: configPath,
language: 'json5',
code: configContents,
codeHighlights: generateJSONCodeHighlights(configContents, [
{
key: keyPath,
type: 'value',
message: md`Cannot find module "${pluginName}"${
alternatives[0]
? `, did you mean "${alternatives[0]}"?`
: ''
}`,
},
]),
},
]
: undefined,
},
});
Expand Down Expand Up @@ -159,16 +163,18 @@ export default async function loadPlugin<T>(
diagnostic: {
message: md`The plugin "${pluginName}" is not compatible with the current version of Parcel. Requires "${parcelVersionRange}" but the current version is "${PARCEL_VERSION}".`,
origin: '@parcel/core',
filePath: pkgFile,
language: 'json5',
codeFrame: {
code: pkgContents,
codeHighlights: generateJSONCodeHighlights(pkgContents, [
{
key: '/engines/parcel',
},
]),
},
codeFrames: [
{
filePath: pkgFile,
language: 'json5',
code: pkgContents,
codeHighlights: generateJSONCodeHighlights(pkgContents, [
{
key: '/engines/parcel',
},
]),
},
],
},
});
}
Expand Down
24 changes: 13 additions & 11 deletions packages/core/core/src/requests/AssetGraphRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,17 +494,19 @@ export class AssetGraphBuilder {
resolution.value.filePath,
)} does not export '${s}'`,
origin: '@parcel/core',
filePath: loc?.filePath,
language: assetNode.value.type,
codeFrame: loc
? {
codeHighlights: [
{
start: loc.start,
end: loc.end,
},
],
}
codeFrames: loc
? [
{
filePath: loc?.filePath,
language: assetNode.value.type,
codeHighlights: [
{
start: loc.start,
end: loc.end,
},
],
},
]
: undefined,
});
}
Expand Down
95 changes: 75 additions & 20 deletions packages/core/core/src/requests/EntryRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ import type {StaticRunOpts} from '../RequestTracker';
import type {Entry, ParcelOptions} from '../types';
import type {FileSystem} from '@parcel/fs';

import {isDirectoryInside, isGlob, glob} from '@parcel/utils';
import ThrowableDiagnostic, {md} from '@parcel/diagnostic';
import {
isDirectoryInside,
isGlob,
glob,
findAlternativeFiles,
} from '@parcel/utils';
import ThrowableDiagnostic, {
md,
generateJSONCodeHighlights,
} from '@parcel/diagnostic';
import path from 'path';

type RunOpts = {|
Expand Down Expand Up @@ -66,26 +74,64 @@ async function run({input, api, options}: RunOpts): Promise<EntryResult> {

async function assertFile(
fs: FileSystem,
source: string,
diagnosticPath: string,
entry: FilePath,
relativeSource: FilePath,
pkgFilePath: FilePath,
keyPath: string,
options: ParcelOptions,
) {
let source = path.join(entry, relativeSource);
let stat;
try {
stat = await fs.stat(source);
} catch (err) {
let contents = await fs.readFile(pkgFilePath, 'utf8');
let alternatives = await findAlternativeFiles(
fs,
relativeSource,
entry,
options.projectRoot,
false,
);
throw new ThrowableDiagnostic({
diagnostic: {
message: `${diagnosticPath} does not exist`,
filePath: source,
origin: '@parcel/core',
message: `${path.relative(process.cwd(), source)} does not exist.`,
codeFrames: [
{
filePath: pkgFilePath,
codeHighlights: generateJSONCodeHighlights(contents, [
{
key: keyPath,
type: 'value',
},
]),
},
],
hints: alternatives.map(r => {
return `Did you mean '__${r}__'?`;
}),
},
});
}

if (!stat.isFile()) {
let contents = await fs.readFile(pkgFilePath, 'utf8');
throw new ThrowableDiagnostic({
diagnostic: {
message: `${diagnosticPath} is not a file`,
filePath: source,
origin: '@parcel/core',
message: `${path.relative(process.cwd(), source)} is not a file.`,
codeFrames: [
{
filePath: pkgFilePath,
codeHighlights: generateJSONCodeHighlights(contents, [
{
key: keyPath,
type: 'value',
},
]),
},
],
},
});
}
Expand Down Expand Up @@ -123,7 +169,6 @@ export class EntryResolver {
throw new ThrowableDiagnostic({
diagnostic: {
message: md`Entry ${entry} does not exist`,
filePath: entry,
},
});
}
Expand All @@ -145,19 +190,26 @@ export class EntryResolver {
let targetSources = Array.isArray(target.source)
? target.source
: [target.source];
let i = 0;
for (let relativeSource of targetSources) {
let source = path.join(entry, relativeSource);
let diagnosticPath = md`${relativeSource} in ${path.relative(
this.options.inputFS.cwd(),
await assertFile(
this.options.inputFS,
entry,
relativeSource,
filePath,
)}#targets["${targetName}"].source`;
await assertFile(this.options.inputFS, source, diagnosticPath);
`/targets/${targetName}/source${
Array.isArray(target.source) ? `/${i}` : ''
}`,
this.options,
);

entries.push({
filePath: source,
packagePath: entry,
target: targetName,
});
i++;
}
}
}
Expand All @@ -173,14 +225,19 @@ export class EntryResolver {
let pkgSources = Array.isArray(pkg.source)
? pkg.source
: [pkg.source];
let i = 0;
for (let pkgSource of pkgSources) {
let source = path.join(path.dirname(filePath), pkgSource);
let diagnosticPath = md`${pkgSource} in ${path.relative(
this.options.inputFS.cwd(),
await assertFile(
this.options.inputFS,
entry,
pkgSource,
filePath,
)}#source`;
await assertFile(this.options.inputFS, source, diagnosticPath);
`/source${Array.isArray(pkg.source) ? `/${i}` : ''}`,
this.options,
);
entries.push({filePath: source, packagePath: entry});
i++;
}
}

Expand All @@ -196,7 +253,6 @@ export class EntryResolver {
throw new ThrowableDiagnostic({
diagnostic: {
message: md`Could not find entry: ${entry}`,
filePath: entry,
},
});
} else if (stat.isFile()) {
Expand All @@ -217,7 +273,6 @@ export class EntryResolver {
throw new ThrowableDiagnostic({
diagnostic: {
message: `Unknown entry: ${entry}`,
filePath: entry,
},
});
}
Expand All @@ -236,13 +291,13 @@ export class EntryResolver {
try {
pkg = JSON.parse(content);
} catch (err) {
// TODO: code frame?
throw new ThrowableDiagnostic({
diagnostic: {
message: md`Error parsing ${path.relative(
this.options.inputFS.cwd(),
pkgFile,
)}: ${err.message}`,
filePath: pkgFile,
},
});
}
Expand Down