Skip to content

Commit

Permalink
fix: 🐛 Windows build compatibility
Browse files Browse the repository at this point in the history
wizer fails in snapshot using windows paths. sdk now forces unix type paths for wizer processing
  • Loading branch information
godronus committed Jan 28, 2025
1 parent 936f02e commit b083c2d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 16 additions & 11 deletions src/componentize/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,28 @@ import { addWasmMetadata } from './add-wasm-metadata.js';
import { getJsInputContents } from './get-js-input.js';
import { precompile } from './precompile.js';

import { getTmpDir, npxPackagePath, resolveTmpDir } from '~utils/file-system.js';
import {
getTmpDir,
npxPackagePath,
resolveOsPath,
resolveTmpDir,
useUnixPath,
} from '~utils/file-system.js';
import { validateFilePaths } from '~utils/input-path-verification.js';

async function componentize(jsInput, output, opts = {}) {
const {
debug = false,
wasmEngine = await npxPackagePath('./lib/fastedge-runtime.wasm'),
wasmEngine = npxPackagePath('./lib/fastedge-runtime.wasm'),
enableStdout = false,
enablePBL = false,
preBundleJSInput = true,
} = opts;

const jsPath = fileURLToPath(new URL(resolve(process.cwd(), jsInput), import.meta.url));
const jsPath = resolveOsPath(process.cwd(), jsInput);

const wasmOutputDir = resolveOsPath(process.cwd(), output);

const wasmOutputDir = fileURLToPath(new URL(resolve(process.cwd(), output), import.meta.url));
await validateFilePaths(jsPath, wasmOutputDir, wasmEngine);

const contents = await getJsInputContents(jsPath, preBundleJSInput);
Expand All @@ -51,14 +58,14 @@ async function componentize(jsInput, output, opts = {}) {
'--inherit-env=true',
'--dir=.',
// '--dir=../', // todo: Farq: NEED to iterate config file and add these paths for static building...
`--dir=${dirname(wizerInput)}`,
`--dir=${useUnixPath(dirname(wizerInput))}`,
'-r _start=wizer.resume',
`-o=${wasmOutputDir}`,
wasmEngine,
`-o=${useUnixPath(wasmOutputDir)}`,
useUnixPath(wasmEngine),
],
{
stdio: [null, process.stdout, process.stderr],
input: wizerInput,
input: useUnixPath(wizerInput),
shell: true,
encoding: 'utf-8',
env: {
Expand All @@ -85,9 +92,7 @@ async function componentize(jsInput, output, opts = {}) {
}

const coreComponent = await readFile(output);
const adapter = fileURLToPath(
new URL(npxPackagePath('./lib/preview1-adapter.wasm'), import.meta.url),
);
const adapter = npxPackagePath('./lib/preview1-adapter.wasm');

const generatedComponent = await componentNew(coreComponent, [
['wasi_snapshot_preview1', await readFile(adapter)],
Expand Down
9 changes: 5 additions & 4 deletions src/fastedge-build/config-build/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import path from 'node:path';
import { pathToFileURL } from 'node:url';

import { createStaticManifest } from './build-manifest/create-static-manifest.js';
import { buildWasm } from './build-wasm.js';

import { normalizeBuildConfig } from '~utils/config-helpers.js';
import { isFile } from '~utils/file-system.js';
import { isFile, resolveOsPath } from '~utils/file-system.js';
import { colorLog } from '~utils/prompts.js';

async function buildFromConfig(config) {
Expand Down Expand Up @@ -36,7 +36,8 @@ async function loadConfig(configPath) {
try {
const configFileExists = await isFile(configPath);
if (configFileExists) {
const { config } = await import(/* webpackChunkName: "config" */ configPath);
const configUrlPath = pathToFileURL(configPath).href;
const { config } = await import(/* webpackChunkName: "config" */ configUrlPath);
return normalizeBuildConfig(config);
}
colorLog('error', `Error: Config file not found at ${configPath}. Skipping build.`);
Expand All @@ -48,7 +49,7 @@ async function loadConfig(configPath) {

async function buildFromConfigFiles(configFilePaths = []) {
for (const configFilePath of configFilePaths) {
const configPath = path.resolve(configFilePath);
const configPath = resolveOsPath(configFilePath);
// Await in loop is correct, it must run sequentially - it overwrites files within each build cycle
// eslint-disable-next-line no-await-in-loop
await buildFromConfig(await loadConfig(configPath));
Expand Down
25 changes: 23 additions & 2 deletions src/utils/file-system.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
import { readdirSync } from 'node:fs';
import { mkdir, mkdtemp, readdir, stat } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import path from 'node:path';
import path, { resolve } from 'node:path';
import { fileURLToPath } from 'node:url';

import { colorLog } from './prompts.js';

/**
*
* Normalizes and resolves the path for unix/windows compatibility
* @param {Array<string>} paths
* @returns {string} path
*/
const resolveOsPath = (...paths) => path.normalize(resolve(...paths));

/**
*
* Replaces backslashes with forward slashes - wizer requires unix paths
* @param {string} path
* @returns {string} path
*/
const useUnixPath = (path) => path.replace(/\\/gu, '/');

/**
*
* @param {string} filePath
* @returns {string} npxPackagePath
*/
const npxPackagePath = (filePath) => {
const __dirname = path.dirname(fileURLToPath(import.meta.url)).replace(/\/bin([^/]*)$/u, '');
const __dirname = path
.dirname(fileURLToPath(import.meta.url))
.replace(/[\\/]bin([\\/][^\\/]*)?$/u, '');

try {
return path.resolve(__dirname, filePath);
} catch {
Expand Down Expand Up @@ -120,5 +139,7 @@ export {
isDirectory,
isFile,
npxPackagePath,
resolveOsPath,
resolveTmpDir,
useUnixPath,
};

0 comments on commit b083c2d

Please sign in to comment.