Skip to content

Commit 935f05a

Browse files
authored
[Rust] Fixed import path rewrite (#3954)
1 parent c1c7ae3 commit 935f05a

File tree

16 files changed

+96
-40
lines changed

16 files changed

+96
-40
lines changed

src/Fable.Cli/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Fixed
1111

12+
* [Rust] Fixed import path rewrite (by @ncave)
1213
* [Rust] Updated derived interfaces (by @ncave)
1314
* [Rust] Updated string comparisons (by @ncave)
1415
* [Rust] Fixed derived traits mapping (by @ncave)

src/Fable.Cli/Pipeline.fs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,21 @@ module Rust =
442442
let stream = new IO.StreamWriter(targetPath)
443443

444444
interface Printer.Writer with
445-
member _.Write(str) =
445+
member self.Write(str) =
446+
447+
let str =
448+
// rewrite import paths in last file
449+
if com.CurrentFile = (Array.last com.SourceFiles) then
450+
System.Text.RegularExpressions.Regex.Replace(
451+
str,
452+
@"(#\[path\s*=\s*\"")([^""]*)(\""])",
453+
fun m ->
454+
let path = (self :> Printer.Writer).MakeImportPath(m.Groups[2].Value)
455+
m.Groups[1].Value + path + m.Groups[3].Value
456+
)
457+
else
458+
str
459+
446460
stream.WriteAsync(str) |> Async.AwaitTask
447461

448462
member _.MakeImportPath(path) =

src/Fable.Compiler/Util.fs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,10 @@ module Imports =
617617
importPath
618618

619619
if isAbsolutePath importPath then
620-
if importPath.EndsWith(".fs", StringComparison.Ordinal) then
620+
if
621+
importPath.EndsWith(".fs", StringComparison.Ordinal)
622+
|| importPath.EndsWith(".rs", StringComparison.Ordinal)
623+
then
621624
getTargetRelativePath pathResolver importPath targetDir projDir outDir
622625
else
623626
getRelativePath targetDir importPath

src/Fable.Transforms/Rust/AST/Rust.AST.Helpers.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module Naming =
2020
let rustPrelude = HashSet(kw.RustPrelude)
2121

2222
let rawIdent (ident: string) =
23-
if ident.StartsWith("r#") then
23+
if ident = "" || ident = "_" || ident.StartsWith("r#") then
2424
ident
2525
else
2626
"r#" + ident

src/Fable.Transforms/Rust/Fable2Rust.fs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2228,9 +2228,8 @@ module Util =
22282228

22292229
let maybeAddParens fableExpr (expr: Rust.Expr) : Rust.Expr =
22302230
match fableExpr with
2231-
| Fable.IfThenElse _ -> mkParenExpr expr
2232-
// TODO: add more expressions that need parens
2233-
| _ -> expr
2231+
| Fable.Value _ -> expr
2232+
| _ -> mkParenExpr expr
22342233

22352234
let transformOperation com ctx range typ opKind : Rust.Expr =
22362235
match opKind with
@@ -5103,6 +5102,7 @@ module Util =
51035102
let isFableLibraryPath (com: IRustCompiler) (path: string) =
51045103
not (isFableLibrary com)
51055104
&& (path.StartsWith(com.LibraryDir, StringComparison.Ordinal)
5105+
|| path.Contains("fable-library-rust")
51065106
|| path = "fable_library_rust")
51075107

51085108
let getImportModulePath (com: IRustCompiler) (path: string) =
@@ -5235,7 +5235,8 @@ module Compiler =
52355235
// add import module to a global list (across files)
52365236
if
52375237
path.Length > 0
5238-
&& path <> "fable_library_rust"
5238+
&& not (path = "fable_library_rust")
5239+
&& not (path.Contains("fable-library-rust"))
52395240
&& not (isFableLibraryPath self path)
52405241
then
52415242
importModules.TryAdd(modulePath, true) |> ignore

src/fable-standalone/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ perf*.data
55
perf*.svg
66
build/
77
dist/
8+
target/
9+
Cargo.lock
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "bench-compiler"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[[bin]]
7+
name = "bench-compiler"
8+
path = "./out-rust/app.rs"
9+
10+
[features]
11+
threaded = ["fable_library_rust/threaded"]
12+
default = ["threaded"]
13+
14+
[dependencies]
15+
fable_library_rust = { path = "./out-rust/fable_modules/fable-library-rust" }

src/fable-standalone/test/bench-compiler/src/Platform.fs renamed to src/fable-standalone/test/bench-compiler/Platform.fs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,26 @@ let getGlobFiles (path: string) =
8484
let serializeToJson (value: obj) =
8585
System.Text.Json.JsonSerializer.Serialize(value)
8686

87-
#else
87+
#endif
88+
89+
#if FABLE_COMPILER_RUST
90+
91+
let readAllBytes (filePath: string) : byte[] = [||]
92+
let readAllText (filePath: string) : string = ""
93+
let writeAllText (filePath: string) (text: string) : unit = ()
94+
let measureTime (f: 'a -> 'b) x = f x, 0
95+
let ensureDirExists (path: string) : unit = ()
96+
let normalizePath (path: string) = path.Replace('\\', '/')
97+
let normalizeFullPath (path: string) = path
98+
let getRelativePath (path: string) (pathTo: string) = path
99+
let getHomePath () = ""
100+
let getDirFiles (path: string) (extension: string) : string[] = [||]
101+
let getGlobFiles (path: string) : string[] = [||]
102+
let serializeToJson (value: obj) = ""
103+
104+
#endif
105+
106+
#if FABLE_COMPILER_JAVASCRIPT || FABLE_COMPILER_TYPESCRIPT
88107

89108
open Fable.Core.JsInterop
90109

src/fable-standalone/test/bench-compiler/src/ProjectParser.fs renamed to src/fable-standalone/test/bench-compiler/ProjectParser.fs

File renamed without changes.

src/fable-standalone/test/bench-compiler/src/app.fs renamed to src/fable-standalone/test/bench-compiler/app.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ open Fable.Compiler.Platform
44
open Fable.Compiler.ProjectParser
55

66
let getMetadataDir () : string =
7-
__SOURCE_DIRECTORY__ + "/../../../../fable-metadata/lib/"
7+
__SOURCE_DIRECTORY__ + "/../../../fable-metadata/lib/"
88

99
let getFableLibDir () : string =
10-
__SOURCE_DIRECTORY__ + "/../../../../../temp/fable-library-js"
10+
__SOURCE_DIRECTORY__ + "/../../../../temp/fable-library-js"
1111

1212
let getVersion () : string = ".next"
1313

0 commit comments

Comments
 (0)