Skip to content

Commit

Permalink
fix(toolchain): fix js-utils build paths on windows (#1720)
Browse files Browse the repository at this point in the history
<!-- Please make sure there is an issue that this PR is correlated to. -->

## Changes

<!-- If there are frontend changes, please include screenshots. -->
  • Loading branch information
NathanFlurry committed Dec 29, 2024
1 parent d45bf55 commit fd525f5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 49 deletions.
47 changes: 16 additions & 31 deletions packages/toolchain/js-utils-embed/build.rs
Original file line number Diff line number Diff line change
@@ -1,56 +1,41 @@
use anyhow::*;
use fs_extra::dir::{copy, CopyOptions};
use merkle_hash::MerkleTree;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;

#[tokio::main]
async fn main() -> Result<()> {
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR")?;
let out_dir = std::env::var("OUT_DIR")?;
let manifest_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR")?);
let out_dir = PathBuf::from(std::env::var("OUT_DIR")?);

let mut js_utils_path = PathBuf::from(manifest_dir.clone());
js_utils_path.push("js");

// Copy js-utils directory to out_dir
let out_js_utils_path = Path::new(&out_dir).join("js-utils");
let out_js_utils_path = out_dir.join("js-utils");

// Remove old dir
if out_js_utils_path.is_dir() {
fs::remove_dir_all(&out_js_utils_path).context("fs::remove_dir_all")?;
}

// Copy js-utils directory to out_dir
// TODO: This breaks deno check``
// let mut copy_options = CopyOptions::new();
// copy_options.overwrite = true;
// copy_options.copy_inside = true;
// copy(&js_utils_path, &out_js_utils_path, &copy_options).with_context(|| {
// format!(
// "failed to copy directory from {} to {}",
// js_utils_path.display(),
// out_js_utils_path.display()
// )
// })?;
// Create the target directory first
let copy_options = CopyOptions::new()
.overwrite(true)
.copy_inside(true);

let status = std::process::Command::new("cp")
.arg("-R")
.arg(&js_utils_path)
.arg(&out_js_utils_path)
.status()
.with_context(|| {
format!(
"failed to copy directory from {} to {}",
js_utils_path.display(),
out_js_utils_path.display()
)
})?;
if !status.success() {
return Err(anyhow!("cp command failed"));
}
copy(&js_utils_path, &out_js_utils_path, &copy_options).with_context(|| {
format!(
"failed to copy directory from {} to {}",
js_utils_path.display(),
out_js_utils_path.display()
)
})?;

// Install deno
let deno_dir = Path::new(&out_dir).join("deno");
let deno_dir = out_dir.join("deno");
let deno_exec = deno_embed::get_executable(&deno_dir).await?;

// Prepare the directory for `include_dir!`
Expand Down
33 changes: 17 additions & 16 deletions packages/toolchain/js-utils-embed/js/src/tasks/build/build.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { resolve } from "@std/path";
import { exists } from "@std/fs";
import { denoPlugins } from "@rivet-gg/esbuild-deno-loader";
import * as esbuild from "esbuild";
import { Input, Output } from "./mod.ts";
Expand All @@ -23,23 +24,23 @@ export async function build(input: Input): Promise<Output> {
// Windows paths, so we manually resolve any paths that
// start with a Windows path separator (\) and resolve
// them to the full path.
// {
// name: "fix-windows-paths",
// setup(build: esbuild.PluginBuild) {
// build.onResolve({ filter: /^\\.*/ }, (args) => {
// const resolvedPath = resolve(args.resolveDir, args.path);
// if (!exists(resolvedPath, { isFile: true })) {
// return {
// errors: [{ text: `File could not be resolved: ${resolvedPath}` }],
// };
// }
{
name: "fix-windows-paths",
setup(build: esbuild.PluginBuild) {
build.onResolve({ filter: /^\\.*/ }, (args) => {
const resolvedPath = resolve(args.resolveDir, args.path);
if (!exists(resolvedPath, { isFile: true })) {
return {
errors: [{ text: `File could not be resolved: ${resolvedPath}` }],
};
}

// return {
// path: resolve(args.resolveDir, args.path),
// };
// });
// },
// } satisfies esbuild.Plugin,
return {
path: resolve(args.resolveDir, args.path),
};
});
},
} satisfies esbuild.Plugin,
],
define: {
// HACK: Disable `process.domain` in order to correctly handle this edge case:
Expand Down
4 changes: 2 additions & 2 deletions packages/toolchain/toolchain/src/tasks/deploy/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ pub async fn build_and_upload(
config::build::javascript::Bundler::Deno => {
// Validate that the script path has a .ts or .js extension
let script_path = project_root.join(&opts.build_config.script);
let ext = script_path.extension().and_then(|s| s.to_str());
ensure!(
script_path.extension().and_then(|s| s.to_str()) == Some("ts")
|| script_path.extension().and_then(|s| s.to_str()) == Some("js"),
ext == Some("ts") || ext == Some("js"),
"script file must have a .ts or .js extension for Deno bundler"
);

Expand Down

0 comments on commit fd525f5

Please sign in to comment.