Skip to content

Commit

Permalink
refactor(@angular/build): show unexpected Sass import resolution errors
Browse files Browse the repository at this point in the history
When attempting to resolve a Sass import, failure to read the contents of a
directory that is not caused by a non-existent directory will now cause
an exception to be thrown. This prevents abnormal situations from being
hidden during the build.

A deprecated Sass interface was also replaced and was a type only change.
  • Loading branch information
clydin committed May 7, 2024
1 parent 5f14787 commit d9053ae
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
15 changes: 12 additions & 3 deletions packages/angular/build/src/tools/sass/rebasing-importer.ts
Expand Up @@ -12,6 +12,7 @@ import { readFileSync, readdirSync, statSync } from 'node:fs';
import { basename, dirname, extname, join, relative } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import type { CanonicalizeContext, Importer, ImporterResult, Syntax } from 'sass';
import { assertIsError } from '../../utils/error';
import { findUrls } from './lexer';

/**
Expand Down Expand Up @@ -224,8 +225,16 @@ export class RelativeUrlRebasingImporter extends UrlRebasingImporter {
let entries;
try {
entries = readdirSync(directory, { withFileTypes: true });
} catch {
return null;
} catch (error) {
assertIsError(error);
// If the containing directory does not exist return null to indicate it cannot be resolved
if (error.code === 'ENOENT') {
return null;
}

throw new Error(`Error reading directory ["${directory}"] while resolving Sass import`, {
cause: error,
});
}

foundDefaults = [];
Expand All @@ -236,7 +245,7 @@ export class RelativeUrlRebasingImporter extends UrlRebasingImporter {
let isFile: boolean;

if (entry.isSymbolicLink()) {
const stats = statSync(join(entry.path, entry.name));
const stats = statSync(join(directory, entry.name));
isDirectory = stats.isDirectory();
isFile = stats.isFile();
} else {
Expand Down
4 changes: 2 additions & 2 deletions packages/angular/build/src/tools/sass/worker.ts
Expand Up @@ -15,7 +15,7 @@ import {
Exception,
FileImporter,
SourceSpan,
StringOptionsWithImporter,
StringOptions,
compileString,
} from 'sass';
import {
Expand Down Expand Up @@ -43,7 +43,7 @@ interface RenderRequestMessage {
/**
* The Sass options to provide to the `dart-sass` compile function.
*/
options: Omit<StringOptionsWithImporter<'sync'>, 'url'> & { url: string };
options: Omit<StringOptions<'sync'>, 'url'> & { url: string };
/**
* Indicates the request has a custom importer function on the main thread.
*/
Expand Down

0 comments on commit d9053ae

Please sign in to comment.