Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -2046,7 +2046,19 @@ function evaluateHostExpressionBindings(
}
});

const bindings = parseHostBindings(hostMetadata);
let bindings: ParsedHostBindings;
try {
bindings = parseHostBindings(hostMetadata);
} catch (e) {
// `parseHostBindings` throws plain `Error`s for malformed bindings (e.g. `'[class.has-]'`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment isn’t needed, please remove.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also seems a bit of an odd choice. Why not change the parsing function to throw fatal diagnostics instead of plain errors if it’s not allowed to throw errors

// typed mid-edit in the language service). Surface them as diagnostics so analysis can
// complete instead of crashing the compiler.
throw new FatalDiagnosticError(
ErrorCode.HOST_BINDING_PARSE_ERROR,
hostExpr,
(e as Error).message,
);
}

const errors = verifyHostBindings(bindings, createSourceSpan(hostExpr));
if (errors.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,11 @@ export class IncrementalCompilation implements IncrementalBuild<ClassRecord, Fil
}

priorTypeCheckingResultsFor(sf: ts.SourceFile): FileTypeCheckingData | null {
// In some environments (e.g., language service during edits), type-check queries can occur

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the implications of this on the language service. cc @atscott

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm well certainly errors are never good for language service if not caught but this seems like a suspicious place to fix it. Probably needs something higher up?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback!

Looking "higher up" in the stack, analyzeTrait in packages/compiler-cli/src/ngtsc/transform/src/compilation.ts already wraps trait.handler.analyze() in a try/catch, but it only handles FatalDiagnosticError and re-throws everything else. That re-throw is what actually propagates up and crashes the Language Service.

Would the right direction be to generalize that catch so any unexpected error during directive analysis becomes a diagnostic instead of being re-thrown? That would protect the LS from this and similar future bugs in any handler, not just host-bindings. Happy to update the PR to take that approach if it sounds right to you.

// before the compiler has fully transitioned to the TypeCheckAndEmit phase. In such cases we
// cannot adopt prior results safely, so return null instead of throwing an assertion.
if (this.phase.kind !== PhaseKind.TypeCheckAndEmit) {
throw new Error(`AssertionError: Expected successfully analyzed compilation.`);
return null;
}

if (this.step === null) {
Expand Down
19 changes: 19 additions & 0 deletions packages/compiler-cli/test/ngtsc/ngtsc_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3256,6 +3256,25 @@ runInEachFileSystem((os: string) => {
);
});

it('should report a diagnostic instead of crashing when a host property binding has a non-static value', () => {
env.tsconfig({});
env.write(
'test.ts',
`
import {Directive} from '@angular/core';

declare function getValue(): string;

@Directive({
selector: 'test-dir',
host: {'[class.foo]': getValue()}
})
export class TestDir {}
`,
);
verifyThrownError(ErrorCode.HOST_BINDING_PARSE_ERROR, 'Property binding must be string');
});

it('should throw error if @Directive.queries field has wrong type', () => {
env.tsconfig({});
env.write(
Expand Down
Loading