From 192a9de0821ab3cd06faa7d1814495f88aaab4d5 Mon Sep 17 00:00:00 2001 From: ncave <777696+ncave@users.noreply.github.com> Date: Tue, 12 Nov 2024 19:54:35 -0800 Subject: [PATCH] [Rust] Fixed import path rewrite --- src/Fable.Cli/CHANGELOG.md | 1 + src/Fable.Cli/Pipeline.fs | 16 +++++- src/Fable.Compiler/Util.fs | 5 +- .../Rust/AST/Rust.AST.Helpers.fs | 2 +- src/Fable.Transforms/Rust/Fable2Rust.fs | 9 +-- src/fable-standalone/.gitignore | 2 + .../test/bench-compiler/Cargo.toml | 15 +++++ .../test/bench-compiler/{src => }/Platform.fs | 21 ++++++- .../bench-compiler/{src => }/ProjectParser.fs | 0 .../test/bench-compiler/{src => }/app.fs | 4 +- .../{src => }/bench-compiler.fsproj | 4 +- .../test/bench-compiler/{src => }/main.mjs | 0 .../test/bench-compiler/package.json | 57 ++++++++++--------- .../test/bench-compiler/{src => }/rd.xml | 0 .../bench-compiler/{src => }/tsconfig.json | 0 .../test/bench-compiler/{src => }/util.js | 0 16 files changed, 96 insertions(+), 40 deletions(-) create mode 100644 src/fable-standalone/test/bench-compiler/Cargo.toml rename src/fable-standalone/test/bench-compiler/{src => }/Platform.fs (90%) rename src/fable-standalone/test/bench-compiler/{src => }/ProjectParser.fs (100%) rename src/fable-standalone/test/bench-compiler/{src => }/app.fs (99%) rename src/fable-standalone/test/bench-compiler/{src => }/bench-compiler.fsproj (93%) rename src/fable-standalone/test/bench-compiler/{src => }/main.mjs (100%) rename src/fable-standalone/test/bench-compiler/{src => }/rd.xml (100%) rename src/fable-standalone/test/bench-compiler/{src => }/tsconfig.json (100%) rename src/fable-standalone/test/bench-compiler/{src => }/util.js (100%) diff --git a/src/Fable.Cli/CHANGELOG.md b/src/Fable.Cli/CHANGELOG.md index 201f982bdb..2548b17b38 100644 --- a/src/Fable.Cli/CHANGELOG.md +++ b/src/Fable.Cli/CHANGELOG.md @@ -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) diff --git a/src/Fable.Cli/Pipeline.fs b/src/Fable.Cli/Pipeline.fs index af45bde624..3b563e18dc 100644 --- a/src/Fable.Cli/Pipeline.fs +++ b/src/Fable.Cli/Pipeline.fs @@ -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) = diff --git a/src/Fable.Compiler/Util.fs b/src/Fable.Compiler/Util.fs index 728c720961..69c02bc66f 100644 --- a/src/Fable.Compiler/Util.fs +++ b/src/Fable.Compiler/Util.fs @@ -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 diff --git a/src/Fable.Transforms/Rust/AST/Rust.AST.Helpers.fs b/src/Fable.Transforms/Rust/AST/Rust.AST.Helpers.fs index 53c93cc4d6..f78ad726f9 100644 --- a/src/Fable.Transforms/Rust/AST/Rust.AST.Helpers.fs +++ b/src/Fable.Transforms/Rust/AST/Rust.AST.Helpers.fs @@ -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 diff --git a/src/Fable.Transforms/Rust/Fable2Rust.fs b/src/Fable.Transforms/Rust/Fable2Rust.fs index c19125fbce..0e042a29ee 100644 --- a/src/Fable.Transforms/Rust/Fable2Rust.fs +++ b/src/Fable.Transforms/Rust/Fable2Rust.fs @@ -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 @@ -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) = @@ -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 diff --git a/src/fable-standalone/.gitignore b/src/fable-standalone/.gitignore index 3c031765b2..0811e65222 100644 --- a/src/fable-standalone/.gitignore +++ b/src/fable-standalone/.gitignore @@ -5,3 +5,5 @@ perf*.data perf*.svg build/ dist/ +target/ +Cargo.lock diff --git a/src/fable-standalone/test/bench-compiler/Cargo.toml b/src/fable-standalone/test/bench-compiler/Cargo.toml new file mode 100644 index 0000000000..53b38d6b1d --- /dev/null +++ b/src/fable-standalone/test/bench-compiler/Cargo.toml @@ -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" } diff --git a/src/fable-standalone/test/bench-compiler/src/Platform.fs b/src/fable-standalone/test/bench-compiler/Platform.fs similarity index 90% rename from src/fable-standalone/test/bench-compiler/src/Platform.fs rename to src/fable-standalone/test/bench-compiler/Platform.fs index 54e168d0a0..b6b698be8a 100644 --- a/src/fable-standalone/test/bench-compiler/src/Platform.fs +++ b/src/fable-standalone/test/bench-compiler/Platform.fs @@ -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 diff --git a/src/fable-standalone/test/bench-compiler/src/ProjectParser.fs b/src/fable-standalone/test/bench-compiler/ProjectParser.fs similarity index 100% rename from src/fable-standalone/test/bench-compiler/src/ProjectParser.fs rename to src/fable-standalone/test/bench-compiler/ProjectParser.fs diff --git a/src/fable-standalone/test/bench-compiler/src/app.fs b/src/fable-standalone/test/bench-compiler/app.fs similarity index 99% rename from src/fable-standalone/test/bench-compiler/src/app.fs rename to src/fable-standalone/test/bench-compiler/app.fs index 6b783d1cd9..4dcf494fcd 100644 --- a/src/fable-standalone/test/bench-compiler/src/app.fs +++ b/src/fable-standalone/test/bench-compiler/app.fs @@ -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" diff --git a/src/fable-standalone/test/bench-compiler/src/bench-compiler.fsproj b/src/fable-standalone/test/bench-compiler/bench-compiler.fsproj similarity index 93% rename from src/fable-standalone/test/bench-compiler/src/bench-compiler.fsproj rename to src/fable-standalone/test/bench-compiler/bench-compiler.fsproj index 24140f5998..6f3c7750cb 100644 --- a/src/fable-standalone/test/bench-compiler/src/bench-compiler.fsproj +++ b/src/fable-standalone/test/bench-compiler/bench-compiler.fsproj @@ -46,8 +46,8 @@ - - + + diff --git a/src/fable-standalone/test/bench-compiler/src/main.mjs b/src/fable-standalone/test/bench-compiler/main.mjs similarity index 100% rename from src/fable-standalone/test/bench-compiler/src/main.mjs rename to src/fable-standalone/test/bench-compiler/main.mjs diff --git a/src/fable-standalone/test/bench-compiler/package.json b/src/fable-standalone/test/bench-compiler/package.json index e3df589712..e44bf4ac94 100644 --- a/src/fable-standalone/test/bench-compiler/package.json +++ b/src/fable-standalone/test/bench-compiler/package.json @@ -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", @@ -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", @@ -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" } } diff --git a/src/fable-standalone/test/bench-compiler/src/rd.xml b/src/fable-standalone/test/bench-compiler/rd.xml similarity index 100% rename from src/fable-standalone/test/bench-compiler/src/rd.xml rename to src/fable-standalone/test/bench-compiler/rd.xml diff --git a/src/fable-standalone/test/bench-compiler/src/tsconfig.json b/src/fable-standalone/test/bench-compiler/tsconfig.json similarity index 100% rename from src/fable-standalone/test/bench-compiler/src/tsconfig.json rename to src/fable-standalone/test/bench-compiler/tsconfig.json diff --git a/src/fable-standalone/test/bench-compiler/src/util.js b/src/fable-standalone/test/bench-compiler/util.js similarity index 100% rename from src/fable-standalone/test/bench-compiler/src/util.js rename to src/fable-standalone/test/bench-compiler/util.js