Skip to content

Commit e6056d3

Browse files
committed
fix(@angular/build): resolve dev-server sslKey and sslCert relative to the workspace root
The Vite-based dev server read `sslKey` and `sslCert` relative to the current working directory, so serving failed with `ENOENT` when `ng serve` ran from a subdirectory of the workspace and the paths in `angular.json` were relative. Resolve both paths against the workspace root, as the webpack dev server already does. The new dev-server `ssl` spec covers relative paths. It fails with `ENOENT` without this change.
1 parent 508d6a2 commit e6056d3

2 files changed

Lines changed: 62 additions & 3 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import { executeDevServer } from '../../index';
10+
import { describeServeBuilder } from '../jasmine-helpers';
11+
import { BASE_OPTIONS, DEV_SERVER_BUILDER_INFO } from '../setup';
12+
13+
const SSL_KEY = `-----BEGIN PRIVATE KEY-----
14+
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgEpythQjrJo9bm8GM
15+
LFacGutCqc/XxDZ3dDkSMYZYZ2ehRANCAARvwtZ3OwP1uLhrcLp7nemvaWqkA+FN
16+
NDjOA3OZKhyVHYog1jNTawJJjwk66upbXGFL8622yU1INWbZFxvrSANl
17+
-----END PRIVATE KEY-----
18+
`;
19+
20+
const SSL_CERT = `-----BEGIN CERTIFICATE-----
21+
MIIBfzCCASWgAwIBAgIUUoRhwzxAE3y5nrOW8de+cJEIVvQwCgYIKoZIzj0EAwIw
22+
FDESMBAGA1UEAwwJbG9jYWxob3N0MCAXDTI2MTAxOTA4NTUyNVoYDzIxMjYwOTI1
23+
MDg1NTI1WjAUMRIwEAYDVQQDDAlsb2NhbGhvc3QwWTATBgcqhkjOPQIBBggqhkjO
24+
PQMBBwNCAARvwtZ3OwP1uLhrcLp7nemvaWqkA+FNNDjOA3OZKhyVHYog1jNTawJJ
25+
jwk66upbXGFL8622yU1INWbZFxvrSANlo1MwUTAdBgNVHQ4EFgQUWyqB1QeFcs6L
26+
LL7Utsybm6+XFvIwHwYDVR0jBBgwFoAUWyqB1QeFcs6LLL7Utsybm6+XFvIwDwYD
27+
VR0TAQH/BAUwAwEB/zAKBggqhkjOPQQDAgNIADBFAiBazry25W+Qewb/WzadhmdY
28+
YDGdshNZCk0g0DI3ctNjlgIhAKm2X8pmqdkM38Yh8QDN0cHBsAcDfRYffDnjJ3tP
29+
dpnU
30+
-----END CERTIFICATE-----
31+
`;
32+
33+
describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupTarget) => {
34+
describe('option: "ssl"', () => {
35+
beforeEach(async () => {
36+
setupTarget(harness);
37+
38+
// Application code is not needed for these tests
39+
await harness.writeFile('src/main.ts', '');
40+
});
41+
42+
it('resolves "sslKey" and "sslCert" relative to the workspace root', async () => {
43+
await harness.writeFile('ssl/server.key', SSL_KEY);
44+
await harness.writeFile('ssl/server.crt', SSL_CERT);
45+
46+
harness.useTarget('serve', {
47+
...BASE_OPTIONS,
48+
ssl: true,
49+
sslKey: 'ssl/server.key',
50+
sslCert: 'ssl/server.crt',
51+
});
52+
53+
const { result } = await harness.executeOnce();
54+
55+
expect(result?.success).toBeTrue();
56+
expect(result?.baseUrl).toMatch(/^https:\/\/[^/]+:\d+\//);
57+
});
58+
});
59+
});

‎packages/angular/build/src/builders/dev-server/vite/server.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import { readFile } from 'node:fs/promises';
10-
import { join } from 'node:path';
10+
import { join, resolve } from 'node:path';
1111
import type * as Vite from 'vite' with {
1212
'resolution-mode': 'import',
1313
};
@@ -85,8 +85,8 @@ async function createServerConfig(
8585

8686
if (serverOptions.ssl && serverOptions.sslCert && serverOptions.sslKey) {
8787
const [cert, key] = await Promise.all([
88-
readFile(serverOptions.sslCert),
89-
readFile(serverOptions.sslKey),
88+
readFile(resolve(serverOptions.workspaceRoot, serverOptions.sslCert)),
89+
readFile(resolve(serverOptions.workspaceRoot, serverOptions.sslKey)),
9090
]);
9191

9292
server.https = {

0 commit comments

Comments
 (0)