Skip to content

Commit 7790115

Browse files
authored
Merge pull request #95 from jl4nz/93-use-fileurltopath
FIx path resolution - including windows
2 parents 49df4ac + 5b3207c commit 7790115

File tree

7 files changed

+69
-40
lines changed

7 files changed

+69
-40
lines changed

.changeset/early-pans-make.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"sst": patch
3+
---
4+
5+
Fix path resolution - including windows

packages/sst/src/credentials.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from "path";
2+
import { fileURLToPath, pathToFileURL } from "url";
23
import type { AwsCredentialIdentityProvider } from "@aws-sdk/types";
34
import { fromNodeProviderChain } from "@aws-sdk/credential-providers";
45
import { GetCallerIdentityCommand, STSClient } from "@aws-sdk/client-sts";
@@ -146,13 +147,19 @@ export function useAWSClient<C extends any>(
146147

147148
export const useAWSProvider = lazy(async () => {
148149
const cdkToolkitUrl = await import.meta.resolve!("@aws-cdk/toolkit-lib");
149-
const cdkToolkitPath = new URL(cdkToolkitUrl).pathname;
150+
const cdkToolkitPath = fileURLToPath(cdkToolkitUrl);
150151
const { SdkProvider } = await import(
151-
path.resolve(cdkToolkitPath, "..", "api", "aws-auth", "sdk-provider.js")
152+
pathToFileURL(
153+
path.resolve(cdkToolkitPath, "..", "api", "aws-auth", "sdk-provider.js")
154+
).href
152155
);
156+
153157
const { IoHelper } = await import(
154-
path.resolve(cdkToolkitPath, "..", "api", "io", "private", "io-helper.js")
158+
pathToFileURL(
159+
path.resolve(cdkToolkitPath, "..", "api", "io", "private", "io-helper.js")
160+
).href
155161
);
162+
156163
const project = useProject();
157164
return new SdkProvider(useAWSCredentialsProvider(), project.config.region!, {
158165
ioHelper: IoHelper.fromActionAwareIoHost({

packages/sst/src/iot.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { IoTClient, DescribeEndpointCommand } from "@aws-sdk/client-iot";
22
import { useAWSClient, useAWSCredentials } from "./credentials.js";
33
import { VisibleError } from "./error.js";
4+
import { lazy } from "./util/lazy.js";
5+
import { Logger } from "./logger.js";
46

57
export const useIOTEndpoint = lazy(async () => {
68
const iot = useAWSClient(IoTClient);
@@ -21,11 +23,8 @@ export const useIOTEndpoint = lazy(async () => {
2123
import iot from "aws-iot-device-sdk";
2224
import { EventPayload, Events, EventTypes, useBus } from "./bus.js";
2325
import { useProject } from "./project.js";
24-
import { Logger } from "./logger.js";
25-
import { randomUUID } from "crypto";
2626
import { useBootstrap } from "./bootstrap.js";
2727
import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
28-
import { lazy } from "./util/lazy.js";
2928

3029
interface Fragment {
3130
id: string;

packages/sst/src/stacks/deploy.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from "path";
2+
import { fileURLToPath, pathToFileURL } from "url";
23
import { useBus } from "../bus.js";
34
import { ConfigOptions, useProject } from "../project.js";
45
import { useAWSClient, useAWSProvider } from "../credentials.js";
@@ -249,12 +250,16 @@ async function addInUseExports(
249250

250251
async function createCdkDeployments() {
251252
const cdkToolkitUrl = await import.meta.resolve!("@aws-cdk/toolkit-lib");
252-
const cdkToolkitPath = new URL(cdkToolkitUrl).pathname;
253+
const cdkToolkitPath = fileURLToPath(cdkToolkitUrl);
253254
const { Deployments } = await import(
254-
path.resolve(cdkToolkitPath, "..", "api", "deployments", "deployments.js")
255+
pathToFileURL(
256+
path.resolve(cdkToolkitPath, "..", "api", "deployments", "deployments.js")
257+
).href
255258
);
256259
const { IoHelper } = await import(
257-
path.resolve(cdkToolkitPath, "..", "api", "io", "private", "io-helper.js")
260+
pathToFileURL(
261+
path.resolve(cdkToolkitPath, "..", "api", "io", "private", "io-helper.js")
262+
).href
258263
);
259264
const provider = await useAWSProvider();
260265
await useAWSProvider();

packages/sst/src/stacks/synth.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { App } from "../constructs/App.js";
33
import { useProject } from "../project.js";
44
import { useAWSProvider, useSTSIdentity } from "../credentials.js";
55
import path from "path";
6+
import { fileURLToPath, pathToFileURL } from "url";
67
import { VisibleError } from "../error.js";
78
import { Semaphore } from "../util/semaphore.js";
89
import { Configuration } from "../util/user-configuration.js";
@@ -24,15 +25,22 @@ export async function synth(opts: SynthOptions) {
2425
const cxapi = await import("@aws-cdk/cx-api");
2526

2627
const cdkToolkitUrl = await import.meta.resolve!("@aws-cdk/toolkit-lib");
27-
const cdkToolkitPath = new URL(cdkToolkitUrl).pathname;
28+
const cdkToolkitPath = fileURLToPath(cdkToolkitUrl);
2829
const { IoHelper } = await import(
29-
path.resolve(cdkToolkitPath, "..", "api", "io", "private", "io-helper.js")
30+
pathToFileURL(
31+
path.resolve(cdkToolkitPath, "..", "api", "io", "private", "io-helper.js")
32+
).href
3033
);
3134
const { PluginHost } = await import(
32-
path.resolve(cdkToolkitPath, "..", "api", "plugin", "plugin.js")
35+
pathToFileURL(
36+
path.resolve(cdkToolkitPath, "..", "api", "plugin", "plugin.js")
37+
).href
3338
);
39+
3440
const { provideContextValues } = await import(
35-
path.resolve(cdkToolkitPath, "..", "context-providers", "index.js")
41+
pathToFileURL(
42+
path.resolve(cdkToolkitPath, "..", "context-providers", "index.js")
43+
).href
3644
);
3745
const project = useProject();
3846

packages/sst/src/util/user-configuration.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,40 @@
1-
// @ts-nocheck
21
import * as os from "os";
32
import * as fs_path from "path";
43
import * as fs from "fs";
4+
import { fileURLToPath, pathToFileURL } from "url";
5+
6+
type Tag = { Key: string; Value: string };
57

68
export const PROJECT_CONFIG = "cdk.json";
79
export const USER_DEFAULTS = "~/.cdk.json";
810
const CONTEXT_KEY = "context";
911

10-
let cdkToolkitPath: string;
12+
let cdkToolkitPath = "";
13+
let cdkToolkitUrl = "";
14+
1115
try {
12-
const cdkToolkitUrl = await import.meta.resolve!("@aws-cdk/toolkit-lib");
13-
cdkToolkitPath = new URL(cdkToolkitUrl).pathname;
16+
cdkToolkitUrl = await import.meta.resolve!("@aws-cdk/toolkit-lib");
17+
cdkToolkitPath = fileURLToPath(cdkToolkitUrl);
1418
} catch (e) {
1519
// Fallback for test environment where import.meta.resolve is not available
1620
const module = await import("module");
1721
const require = (module as any).createRequire(import.meta.url);
1822
cdkToolkitPath = require.resolve("@aws-cdk/toolkit-lib");
23+
cdkToolkitUrl = pathToFileURL(cdkToolkitPath).href;
1924
}
20-
const { ToolkitError } = await import(cdkToolkitPath);
25+
26+
const { ToolkitError } = await import(cdkToolkitUrl);
2127
const { Context, PROJECT_CONTEXT } = await import(
22-
fs_path.resolve(cdkToolkitPath, "..", "api", "context.js")
28+
pathToFileURL(fs_path.resolve(cdkToolkitPath, "..", "api", "context.js")).href
2329
);
2430
const { Settings } = await import(
25-
fs_path.resolve(cdkToolkitPath, "..", "api", "settings.js")
31+
pathToFileURL(fs_path.resolve(cdkToolkitPath, "..", "api", "settings.js"))
32+
.href
2633
);
2734
const { Tags } = await import(
28-
fs_path.resolve(cdkToolkitPath, "..", "api", "tags", "index.js")
35+
pathToFileURL(
36+
fs_path.resolve(cdkToolkitPath, "..", "api", "tags", "index.js")
37+
).href
2938
);
3039

3140
export enum Command {
@@ -103,10 +112,10 @@ export class Configuration {
103112
output: "cdk.out",
104113
});
105114

106-
private readonly commandLineArguments: Settings;
107-
private readonly commandLineContext: Settings;
108-
private _projectConfig?: Settings;
109-
private _projectContext?: Settings;
115+
private readonly commandLineArguments: typeof Settings;
116+
private readonly commandLineContext: typeof Settings;
117+
private _projectConfig?: typeof Settings;
118+
private _projectContext?: typeof Settings;
110119
private loaded = false;
111120

112121
constructor(private readonly props: ConfigurationProps = {}) {
@@ -192,11 +201,11 @@ export class Configuration {
192201
}
193202
}
194203

195-
async function loadAndLog(fileName: string): Promise<Settings> {
204+
async function loadAndLog(fileName: string): Promise<typeof Settings> {
196205
return await settingsFromFile(fileName);
197206
}
198207

199-
async function settingsFromFile(fileName: string): Promise<Settings> {
208+
async function settingsFromFile(fileName: string): Promise<typeof Settings> {
200209
let settings;
201210
const expanded = expandHomeDir(fileName);
202211
if (fs.existsSync(expanded)) {
@@ -218,7 +227,7 @@ async function settingsFromFile(fileName: string): Promise<Settings> {
218227
}
219228

220229
function prohibitContextKeys(
221-
settings: Settings,
230+
settings: typeof Settings,
222231
keys: string[],
223232
fileName: string
224233
) {
@@ -239,7 +248,7 @@ function prohibitContextKeys(
239248
}
240249

241250
function warnAboutContextKey(
242-
settings: Settings,
251+
settings: typeof Settings,
243252
prefix: string,
244253
fileName: string
245254
) {

packages/sst/test/constructs/helper.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
import { Match, Matcher, MatchResult, Template } from "aws-cdk-lib/assertions";
22
import { Stack } from "aws-cdk-lib";
3-
import { App, AppDeployProps, AppProps } from "../../dist/constructs";
3+
import { App, AppDeployProps } from "../../dist/constructs";
44
import { setProject, useProject } from "../../dist/project.js";
5-
import { useNodeHandler } from "../../dist/runtime/handlers/node.js";
6-
import { usePythonHandler } from "../../dist/runtime/handlers/python.js";
7-
import { useDotnetHandler } from "../../dist/runtime/handlers/dotnet.js";
8-
import { useJavaHandler } from "../../dist/runtime/handlers/java.js";
9-
import { useGoHandler } from "../../dist/runtime/handlers/go.js";
10-
import { useContainerHandler } from "../../dist/runtime/handlers/container.js";
11-
import os from "os";
12-
import path from "path";
13-
import fs from "fs/promises";
14-
import crypto from "crypto";
5+
6+
import * as os from "os";
7+
import * as path from "path";
8+
import * as fs from "fs/promises";
9+
import * as crypto from "crypto";
1510

1611
///////////////////////
1712
// Matcher functions //
@@ -34,6 +29,7 @@ export async function createApp(props?: Partial<AppDeployProps>) {
3429
setProject({
3530
version: "test",
3631
cdkVersion: "test",
32+
constructsVersion: "test",
3733
stacks: async () => {},
3834
metafile: null as any,
3935
config: {

0 commit comments

Comments
 (0)