Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: #[ts(export_to = "../path")] can cause diff_paths to fail #247

Merged
merged 8 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ts-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ serde = { version = "1.0", features = ["derive"] }
chrono = { version = "0.4", features = ["serde"] }

[dependencies]
path-clean = "1"
heapless = { version = "0.7", optional = true }
ts-rs-macros = { version = "7.1.1", path = "../macros" }
dprint-plugin-typescript = { version = "0.85.1", optional = true }
Expand Down
15 changes: 12 additions & 3 deletions ts-rs/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
sync::{Mutex, OnceLock},
};

use path_clean::PathClean;
use thiserror::Error;
use ExportError::*;

Expand Down Expand Up @@ -221,6 +222,14 @@ fn import_path(from: &Path, import: &Path) -> String {
}
}

fn to_absolute_path(path: &Path) -> PathBuf {
let Ok(current_path) = std::env::current_dir() else {
return path.to_owned()
};
escritorio-gustavo marked this conversation as resolved.
Show resolved Hide resolved

current_path.join(path).clean()
escritorio-gustavo marked this conversation as resolved.
Show resolved Hide resolved
}

// Construct a relative path from a provided base directory path to the provided path.
//
// Copyright 2012-2015 The Rust Project Developers.
Expand All @@ -238,12 +247,12 @@ where
P: AsRef<Path>,
B: AsRef<Path>,
{
let path = path.as_ref();
let base = base.as_ref();
let path = to_absolute_path(path.as_ref());
let base = to_absolute_path(base.as_ref());

if path.is_absolute() != base.is_absolute() {
if path.is_absolute() {
Some(PathBuf::from(path))
Some(path)
} else {
None
}
escritorio-gustavo marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
14 changes: 14 additions & 0 deletions ts-rs/tests/path_bug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#![allow(dead_code)]
use ts_rs::TS;

#[derive(TS)]
#[ts(export, export_to = "../ts-rs/tests-out/path_bug/")]
struct Foo {
bar: Bar
}

#[derive(TS)]
#[ts(export_to = "tests-out/path_bug/aaa/")]
struct Bar {
i: i32
}
Loading