1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

fix(compile/windows): handle cjs re-export of relative path with parent component (#24795)

Closes https://github.com/denoland/deno/issues/24785
This commit is contained in:
David Sherret 2024-07-30 16:46:15 -04:00 committed by GitHub
parent 7a3810195d
commit fe884c557a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 94 additions and 1 deletions

View file

@ -13,8 +13,31 @@ pub trait PathClean<T> {
impl PathClean<PathBuf> for PathBuf { impl PathClean<PathBuf> for PathBuf {
fn clean(&self) -> PathBuf { fn clean(&self) -> PathBuf {
fn is_clean_path(path: &Path) -> bool {
let path = path.to_string_lossy();
let mut current_index = 0;
while let Some(index) = path[current_index..].find("\\.") {
let trailing_index = index + current_index + 2;
let mut trailing_chars = path[trailing_index..].chars();
match trailing_chars.next() {
Some('.') => match trailing_chars.next() {
Some('/') | Some('\\') | None => {
return false;
}
_ => {}
},
Some('/') | Some('\\') => {
return false;
}
_ => {}
}
current_index = trailing_index;
}
true
}
let path = path_clean::PathClean::clean(self); let path = path_clean::PathClean::clean(self);
if cfg!(windows) && path.to_string_lossy().contains("..\\") { if cfg!(windows) && !is_clean_path(&path) {
// temporary workaround because path_clean::PathClean::clean is // temporary workaround because path_clean::PathClean::clean is
// not good enough on windows // not good enough on windows
let mut components = Vec::new(); let mut components = Vec::new();
@ -103,6 +126,20 @@ pub fn strip_unc_prefix(path: PathBuf) -> PathBuf {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
#[cfg(windows)]
#[test]
fn test_path_clean() {
use super::*;
run_test("C:\\test\\./file.txt", "C:\\test\\file.txt");
run_test("C:\\test\\../other/file.txt", "C:\\other\\file.txt");
run_test("C:\\test\\../other\\file.txt", "C:\\other\\file.txt");
fn run_test(input: &str, expected: &str) {
assert_eq!(PathBuf::from(input).clean(), PathBuf::from(expected));
}
}
#[cfg(windows)] #[cfg(windows)]
#[test] #[test]
fn test_strip_unc_prefix() { fn test_strip_unc_prefix() {

View file

@ -0,0 +1,7 @@
"use strict";
class Hello {
sayHello() {
console.log("Hi.");
}
}
exports.hello = new Hello();

View file

@ -0,0 +1,5 @@
{
"name": "@denotest/cjs-reexport-relative-parent",
"version": "1.0.0",
"main": "./sub_dir/index.js"
}

View file

@ -0,0 +1,19 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
// collision will occur with __esModule in other_file.js
Object.defineProperty(exports, "__esModule", { value: true });
const other_file_1 = __importDefault(require("../dir"));
__exportStar(require("../dir"), exports);
exports.default = other_file_1.default;

View file

@ -0,0 +1,22 @@
{
"tempDir": true,
"steps": [{
"if": "unix",
"args": "compile --output main main.ts",
"output": "[WILDCARD]"
}, {
"if": "unix",
"commandName": "./main",
"args": [],
"output": "main.out"
}, {
"if": "windows",
"args": "compile --output main.exe main.ts",
"output": "[WILDCARD]"
}, {
"if": "windows",
"commandName": "./main.exe",
"args": [],
"output": "main.out"
}]
}

View file

@ -0,0 +1,2 @@
import { hello } from "npm:@denotest/cjs-reexport-relative-parent";
hello.sayHello();