Skip to content

Commit

Permalink
[Rust] Fixed import path rewrite (#3954)
Browse files Browse the repository at this point in the history
  • Loading branch information
ncave authored Nov 13, 2024
1 parent c1c7ae3 commit 935f05a
Show file tree
Hide file tree
Showing 16 changed files with 96 additions and 40 deletions.
1 change: 1 addition & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

* [Rust] Fixed import path rewrite (by @ncave)
* [Rust] Updated derived interfaces (by @ncave)
* [Rust] Updated string comparisons (by @ncave)
* [Rust] Fixed derived traits mapping (by @ncave)
Expand Down
16 changes: 15 additions & 1 deletion src/Fable.Cli/Pipeline.fs
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,21 @@ module Rust =
let stream = new IO.StreamWriter(targetPath)

interface Printer.Writer with
member _.Write(str) =
member self.Write(str) =

let str =
// rewrite import paths in last file
if com.CurrentFile = (Array.last com.SourceFiles) then
System.Text.RegularExpressions.Regex.Replace(
str,
@"(#\[path\s*=\s*\"")([^""]*)(\""])",
fun m ->
let path = (self :> Printer.Writer).MakeImportPath(m.Groups[2].Value)
m.Groups[1].Value + path + m.Groups[3].Value
)
else
str

stream.WriteAsync(str) |> Async.AwaitTask

member _.MakeImportPath(path) =
Expand Down
5 changes: 4 additions & 1 deletion src/Fable.Compiler/Util.fs
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,10 @@ module Imports =
importPath

if isAbsolutePath importPath then
if importPath.EndsWith(".fs", StringComparison.Ordinal) then
if
importPath.EndsWith(".fs", StringComparison.Ordinal)
|| importPath.EndsWith(".rs", StringComparison.Ordinal)
then
getTargetRelativePath pathResolver importPath targetDir projDir outDir
else
getRelativePath targetDir importPath
Expand Down
2 changes: 1 addition & 1 deletion src/Fable.Transforms/Rust/AST/Rust.AST.Helpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Naming =
let rustPrelude = HashSet(kw.RustPrelude)

let rawIdent (ident: string) =
if ident.StartsWith("r#") then
if ident = "" || ident = "_" || ident.StartsWith("r#") then
ident
else
"r#" + ident
Expand Down
9 changes: 5 additions & 4 deletions src/Fable.Transforms/Rust/Fable2Rust.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2228,9 +2228,8 @@ module Util =

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

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

let getImportModulePath (com: IRustCompiler) (path: string) =
Expand Down Expand Up @@ -5235,7 +5235,8 @@ module Compiler =
// add import module to a global list (across files)
if
path.Length > 0
&& path <> "fable_library_rust"
&& not (path = "fable_library_rust")
&& not (path.Contains("fable-library-rust"))
&& not (isFableLibraryPath self path)
then
importModules.TryAdd(modulePath, true) |> ignore
Expand Down
2 changes: 2 additions & 0 deletions src/fable-standalone/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ perf*.data
perf*.svg
build/
dist/
target/
Cargo.lock
15 changes: 15 additions & 0 deletions src/fable-standalone/test/bench-compiler/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "bench-compiler"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "bench-compiler"
path = "./out-rust/app.rs"

[features]
threaded = ["fable_library_rust/threaded"]
default = ["threaded"]

[dependencies]
fable_library_rust = { path = "./out-rust/fable_modules/fable-library-rust" }
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,26 @@ let getGlobFiles (path: string) =
let serializeToJson (value: obj) =
System.Text.Json.JsonSerializer.Serialize(value)

#else
#endif

#if FABLE_COMPILER_RUST

let readAllBytes (filePath: string) : byte[] = [||]
let readAllText (filePath: string) : string = ""
let writeAllText (filePath: string) (text: string) : unit = ()
let measureTime (f: 'a -> 'b) x = f x, 0
let ensureDirExists (path: string) : unit = ()
let normalizePath (path: string) = path.Replace('\\', '/')
let normalizeFullPath (path: string) = path
let getRelativePath (path: string) (pathTo: string) = path
let getHomePath () = ""
let getDirFiles (path: string) (extension: string) : string[] = [||]
let getGlobFiles (path: string) : string[] = [||]
let serializeToJson (value: obj) = ""

#endif

#if FABLE_COMPILER_JAVASCRIPT || FABLE_COMPILER_TYPESCRIPT

open Fable.Core.JsInterop

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ open Fable.Compiler.Platform
open Fable.Compiler.ProjectParser

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

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

let getVersion () : string = ".next"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="../../../../Fable.Core/Fable.Core.fsproj" />
<ProjectReference Include="../../../src/Fable.Standalone.fsproj" />
<ProjectReference Include="../../../Fable.Core/Fable.Core.fsproj" />
<ProjectReference Include="../../src/Fable.Standalone.fsproj" />
</ItemGroup>

<ItemGroup>
Expand Down
57 changes: 29 additions & 28 deletions src/fable-standalone/test/bench-compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,45 @@

"prebuild-lib-ts": "mkdir -p out-lib-ts && cp -r ../../../fable-library-ts/*.ts out-lib-ts && cp -r ../../../fable-library-ts/lib out-lib-ts",
"build-lib-ts": "npm run $FABLE -- ../../../fable-library-ts/Fable.Library.TypeScript.fsproj --outDir ./out-lib-ts --fableLib ./out-lib-ts --lang TypeScript --exclude Fable.Core",
"postbuild-lib-ts": "cp src/tsconfig.json out-lib-ts && npm run tsc -- -p ./out-lib-ts --outDir ./out-lib-js",
"postbuild-lib-ts": "cp tsconfig.json out-lib-ts && npm run tsc -- -p ./out-lib-ts --outDir ./out-lib-js",

"fable-cli": "dotnet run -c Release --project ../../../Fable.Cli",
"prebuild-cli-js": "npm run clean && FABLE=fable-cli npm run build-lib-ts",
"build-cli-js": "npm run fable-cli -- src/bench-compiler.fsproj --outDir ./out-node --fableLib ./out-lib-js",
"build-cli-js": "npm run fable-cli -- bench-compiler.fsproj --outDir ./out-node --fableLib ./out-lib-js",
"postbuild-cli-js": "npm run rollup-bundle",
"build-cli-rust": "npm run fable-cli -- src/bench-compiler.fsproj --outDir ./out-rust --lang Rust --noCache --test:MSBuildCracker",
"build-cli-rust": "npm run fable-cli -- bench-compiler.fsproj --outDir ./out-rust --lang Rust --noCache --test:MSBuildCracker",

"fable": "dotnet run -c Release --project src/bench-compiler.fsproj",
"fable": "dotnet run -c Release --project bench-compiler.fsproj",
"prebuild-js": "npm run clean && FABLE=fable npm run build-lib-ts",
"prebuild-ts": "npm run clean && FABLE=fable npm run build-lib-ts",
"build-js": "npm run fable -- src/bench-compiler.fsproj --outDir ./out-node --fableLib ./out-lib-js",
"build-ts": "npm run fable -- src/bench-compiler.fsproj --outDir ./out-ts --fableLib ./out-lib-ts --lang TypeScript",
"build-rust": "npm run fable -- src/bench-compiler.fsproj --outDir ./out-rust --lang Rust",
"build-js": "npm run fable -- bench-compiler.fsproj --outDir ./out-node --fableLib ./out-lib-js",
"build-ts": "npm run fable -- bench-compiler.fsproj --outDir ./out-ts --fableLib ./out-lib-ts --lang TypeScript",
"build-rust": "npm run fable -- bench-compiler.fsproj --outDir ./out-rust --lang Rust",
"build-opt": "npm run build-js -- --optimize",
"postbuild-js": "npm run rollup-bundle",
"postbuild-ts": "cp src/tsconfig.json out-ts && npm run tsc -- -p ./out-ts --outDir ./out-js",
"postbuild-ts": "cp tsconfig.json out-ts && npm run tsc -- -p ./out-ts --outDir ./out-js",
"postbuild-rust": "cp Cargo.toml out-rust",

"fable-node": "node --stack_size=1200 out-node/app.js",
"fable-bundle": "node --stack_size=1200 dist/bundle.js",
"build-node": "npm run fable-node -- src/bench-compiler.fsproj --outDir ./out-node2 --fableLib ./out-lib-js",
"build-bundle": "npm run fable-bundle -- src/bench-compiler.fsproj --outDir ./out-node2 --fableLib ./out-lib-js",
"benchmark-node": "npm run fable-node -- src/bench-compiler.fsproj --outDir ./out-node2 --benchmark",
"benchmark-bundle": "npm run fable-bundle -- src/bench-compiler.fsproj --outDir ./out-node2 --benchmark",
"build-node": "npm run fable-node -- bench-compiler.fsproj --outDir ./out-node2 --fableLib ./out-lib-js",
"build-bundle": "npm run fable-bundle -- bench-compiler.fsproj --outDir ./out-node2 --fableLib ./out-lib-js",
"benchmark-node": "npm run fable-node -- bench-compiler.fsproj --outDir ./out-node2 --benchmark",
"benchmark-bundle": "npm run fable-bundle -- bench-compiler.fsproj --outDir ./out-node2 --benchmark",

"publish-native": "cd src && dotnet publish -c Release -r linux-x64 & echo \u001B[35m Needs PublishAot enabled in project!",
"prefable-native": "npm run publish-native",
"fable-native": "src/bin/Release/net8.0/linux-x64/native/bench-compiler",
"fable-native": "./bin/Release/net8.0/linux-x64/native/bench-compiler",
"prebuild-native-js": "FABLE=fable-native npm run build-lib-ts",
"build-native-js": "npm run fable-native -- src/bench-compiler.fsproj --outDir ./out-node --fableLib ./out-lib-js",
"build-native-js": "npm run fable-native -- bench-compiler.fsproj --outDir ./out-node --fableLib ./out-lib-js",
"build-test-native-js": "npm run fable-native -- ../../../../../fable-test/fable-test.fsproj --outDir ./out-test",
"build-tests-native-js": "npm run fable-native -- ../../../../tests/Js/Main/Fable.Tests.fsproj --outDir ./out-tests",

"publish-wasm": "cd src && dotnet publish -c Release /p:RunAOTCompilation=true /p:RuntimeIdentifier=browser-wasm",
"prefable-wasm": "npm run publish-wasm",
"fable-wasm": "node src/bin/Release/net8.0/browser-wasm/AppBundle/main.mjs",
"fable-wasm": "node ./bin/Release/net8.0/browser-wasm/AppBundle/main.mjs",
"prebuild-wasm-js": "FABLE=fable-wasm npm run build-lib-ts",
"build-wasm-js": "npm run fable-wasm -- src/bench-compiler.fsproj --outDir ./out-node --fableLib ./out-lib-js",
"build-wasm-js": "npm run fable-wasm -- bench-compiler.fsproj --outDir ./out-node --fableLib ./out-lib-js",

"rollup-bundle": "npm run rollup -- out-node/app.js -o dist/bundle.js --format esm",
"terser-bundle": "npm run terser -- dist/bundle.js -o dist/bundle.min.js --mangle --compress",
Expand All @@ -58,7 +59,7 @@
"build-test-node-js": "npm run fable-node -- ../../../../../fable-test/fable-test.fsproj --outDir ./out-test --fableLib ./out-lib-js --sourceMaps",
"build-test-node-ts": "npm run build-test-node --lang TypeScript",
"postbuild-test-js": "node ./out-test/src/main.js",
"postbuild-test-ts": "cp src/tsconfig.json out-test-ts && npm run tsc -- -p out-test-ts --outDir ./out-test-js",
"postbuild-test-ts": "cp tsconfig.json out-test-ts && npm run tsc -- -p out-test-ts --outDir ./out-test-js",

"prebuild-tests-js": "npm run clean && FABLE=fable npm run build-lib-ts",
"prebuild-tests-ts": "npm run clean && FABLE=fable npm run build-lib-ts",
Expand All @@ -85,24 +86,24 @@
"webpack": "node ../../../../node_modules/webpack-cli/bin/cli.js",
"splitter": "node ../../../../node_modules/fable-splitter/dist/cli",

"perf": "perf record -q -e cpu-clock -F 99 -g -- node --perf-basic-prof --interpreted-frames-native-stack dist/bundle.js src/bench-compiler.fsproj --outDir ./out-node2 --benchmark",
"perf-es": "perf record -q -e cpu-clock -F 99 -g -- node --perf-basic-prof --interpreted-frames-native-stack ./out-node/app.js src/bench-compiler.fsproj --outDir ./out-node2 --benchmark",
"perf-native": "perf record -q -e cpu-clock -F 997 -g -- ./src/bin/Release/net8.0/linux-x64/native/bench-compiler src/bench-compiler.fsproj --outDir ./out-node2 --benchmark",
"perf": "perf record -q -e cpu-clock -F 99 -g -- node --perf-basic-prof --interpreted-frames-native-stack dist/bundle.js bench-compiler.fsproj --outDir ./out-node2 --benchmark",
"perf-es": "perf record -q -e cpu-clock -F 99 -g -- node --perf-basic-prof --interpreted-frames-native-stack ./out-node/app.js bench-compiler.fsproj --outDir ./out-node2 --benchmark",
"perf-native": "perf record -q -e cpu-clock -F 997 -g -- ./bin/Release/net8.0/linux-x64/native/bench-compiler bench-compiler.fsproj --outDir ./out-node2 --benchmark",
"perf-report": "perf report -n --stdio -g srcline -s dso,sym,srcline --inline > perf-report.log",
"perf-script": "perf script -F +pid > perf-script.perf",
"profile": "node --prof out-node/app.js src/bench-compiler.fsproj --outDir ./out-node2 --benchmark",
"cpu-prof": "node --cpu-prof --cpu-prof-dir=out-prof out-node/app.js src/bench-compiler.fsproj --outDir ./out-node2 --benchmark",
"heap-prof": "node --heap-prof out-node/app.js src/bench-compiler.fsproj --outDir ./out-node2 --benchmark",
"profile": "node --prof out-node/app.js bench-compiler.fsproj --outDir ./out-node2 --benchmark",
"cpu-prof": "node --cpu-prof --cpu-prof-dir=out-prof out-node/app.js bench-compiler.fsproj --outDir ./out-node2 --benchmark",
"heap-prof": "node --heap-prof out-node/app.js bench-compiler.fsproj --outDir ./out-node2 --benchmark",
"prof-process": "node --prof-process isolate-*.log > profile.log",
"prof-preprocess": "node --prof-process --preprocess isolate-*.log > profile.v8log.json",
"speedscope": "speedscope profile.v8log.json",
"flamegraph": "perf script | ../../../../../FlameGraph/stackcollapse-perf.pl | ../../../../../FlameGraph/flamegraph.pl > perf.svg",
"trace-node": "node --trace-deopt out-node/app.js src/bench-compiler.fsproj --outDir ./out-node2 > deopt.log",
"trace-rust": "dotnet trace collect --duration 00:00:03:00 --format speedscope -- dotnet src/bin/Release/net8.0/bench-compiler.dll src/bench-compiler.fsproj --outDir ./out-rust2 --fableLib ./out-lib-rust --lang Rust",
"trace-rust-tests": "dotnet trace collect --duration 00:00:01:00 --format speedscope -- dotnet src/bin/Release/net8.0/bench-compiler.dll ../../../../tests/Rust/Fable.Tests.Rust.fsproj --outDir ./out-tests-rust --fableLib ./out-lib-rust --lang Rust",
"heaptrack-native": "heaptrack ./src/bin/Release/net8.0/linux-x64/native/bench-compiler src/bench-compiler.fsproj --outDir ./out-node2 --benchmark",
"trace-node": "node --trace-deopt out-node/app.js bench-compiler.fsproj --outDir ./out-node2 > deopt.log",
"trace-rust": "dotnet trace collect --duration 00:00:03:00 --format speedscope -- dotnet ./bin/Release/net8.0/bench-compiler.dll bench-compiler.fsproj --outDir ./out-rust2 --fableLib ./out-lib-rust --lang Rust",
"trace-rust-tests": "dotnet trace collect --duration 00:00:01:00 --format speedscope -- dotnet ./bin/Release/net8.0/bench-compiler.dll ../../../../tests/Rust/Fable.Tests.Rust.fsproj --outDir ./out-tests-rust --fableLib ./out-lib-rust --lang Rust",
"heaptrack-native": "heaptrack ./bin/Release/net8.0/linux-x64/native/bench-compiler bench-compiler.fsproj --outDir ./out-node2 --benchmark",
"heaptrack-print": "heaptrack_print heaptrack.*.gz -F heap_alloc.log",
"heaptrack-flamegraph": "../../../../../FlameGraph/flamegraph.pl --title \"heaptrack: allocations\" --colors mem --countname allocations < heap_alloc.log > heap_alloc.svg",
"coz": "coz run --- ./src/bin/Release/net8.0/linux-x64/native/bench-compiler src/bench-compiler.fsproj --outDir ./out-node2 --benchmark"
"coz": "coz run --- ./bin/Release/net8.0/linux-x64/native/bench-compiler bench-compiler.fsproj --outDir ./out-node2 --benchmark"
}
}

0 comments on commit 935f05a

Please sign in to comment.