Skip to content

Commit 3924c3d

Browse files
fix(api.js): fix os.platform return on macos and windows, closes #2698 (#2699)
Co-authored-by: FabianLars <fabianlars@fabianlars.de>
1 parent 11db96e commit 3924c3d

File tree

6 files changed

+72
-46
lines changed

6 files changed

+72
-46
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"api": patch
3+
---
4+
5+
Fix `os.platform` returning `macos` and `windows` instead of `darwin` and `win32`.

core/tauri/src/api/file/extract.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ fn detect_archive_type(path: &path::Path) -> ArchiveFormat {
3939
Some(extension) if extension == std::ffi::OsStr::new("tar") => ArchiveFormat::Tar(None),
4040
Some(extension) if extension == std::ffi::OsStr::new("gz") => match path
4141
.file_stem()
42-
.map(|e| path::Path::new(e))
42+
.map(path::Path::new)
4343
.and_then(|f| f.extension())
4444
{
4545
Some(extension) if extension == std::ffi::OsStr::new("tar") => {

core/tauri/src/app.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -705,10 +705,11 @@ impl<R: Runtime> Builder<R> {
705705
T: Send + Sync + 'static,
706706
{
707707
let type_name = std::any::type_name::<T>();
708-
if !self.state.set(state) {
709-
panic!("state for type '{}' is already being managed", type_name);
710-
}
711-
708+
assert!(
709+
self.state.set(state),
710+
"state for type '{}' is already being managed",
711+
type_name
712+
);
712713
self
713714
}
714715

core/tauri/src/endpoints/operating_system.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,32 @@ impl Cmd {
2121
pub fn run(self) -> crate::Result<InvokeResponse> {
2222
#[cfg(os_all)]
2323
return match self {
24-
Self::Platform => Ok(std::env::consts::OS.into()),
24+
Self::Platform => Ok(os_platform().into()),
2525
Self::Version => Ok(os_info::get().version().to_string().into()),
26-
Self::Type => {
27-
#[cfg(target_os = "linux")]
28-
return Ok("Linux".into());
29-
#[cfg(target_os = "windows")]
30-
return Ok("Windows_NT".into());
31-
#[cfg(target_os = "macos")]
32-
return Ok("Darwing".into());
33-
}
26+
Self::Type => Ok(os_type().into()),
3427
Self::Arch => Ok(std::env::consts::ARCH.into()),
3528
Self::Tempdir => Ok(std::env::temp_dir().into()),
3629
};
3730
#[cfg(not(os_all))]
3831
Err(crate::Error::ApiNotAllowlisted("os".into()))
3932
}
4033
}
34+
35+
#[cfg(os_all)]
36+
fn os_type() -> String {
37+
#[cfg(target_os = "linux")]
38+
return "Linux".into();
39+
#[cfg(target_os = "windows")]
40+
return "Windows_NT".into();
41+
#[cfg(target_os = "macos")]
42+
return "Darwin".into();
43+
}
44+
#[cfg(os_all)]
45+
fn os_platform() -> String {
46+
match std::env::consts::OS {
47+
"windows" => "win32",
48+
"macos" => "darwin",
49+
_ => std::env::consts::OS,
50+
}
51+
.into()
52+
}

tooling/api/src/os.ts

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,26 @@ import { invokeTauriCommand } from './helpers/tauri'
3434
* */
3535
const EOL = isWindows() ? '\r\n' : '\n'
3636

37-
type Platform = LiteralUnion<
38-
'aix' | 'darwin' | 'freebsd' | 'linux' | 'openbsd' | 'sunos' | 'win32',
39-
string
40-
>
41-
4237
/**
4338
* Returns a string identifying the operating system platform.
44-
* The value is set at compile time. Possible values are `'aix'`, `'darwin'`, `'freebsd'`, `'linux'`, `'openbsd'`, `'sunos'`, and `'win32'`.
39+
* The value is set at compile time. Possible values are `'linux'`, `'darwin'`, `'ios'`, `'freebsd'`, `'dragonfly'`, `'netbsd'`, `'openbsd'`, `'solaris'`, `'android'`, `'win32'`
4540
*/
46-
async function platform(): Promise<Platform> {
47-
return invokeTauriCommand<Platform>({
41+
async function platform(): Promise<
42+
LiteralUnion<
43+
| 'linux'
44+
| 'darwin'
45+
| 'ios'
46+
| 'freebsd'
47+
| 'dragonfly'
48+
| 'netbsd'
49+
| 'openbsd'
50+
| 'solaris'
51+
| 'android'
52+
| 'win32',
53+
string
54+
>
55+
> {
56+
return invokeTauriCommand<string>({
4857
__tauriModule: 'Os',
4958
message: {
5059
cmd: 'platform'
@@ -64,40 +73,40 @@ async function version(): Promise<string> {
6473
})
6574
}
6675

67-
type OsType = LiteralUnion<'Linux' | 'Darwin' | 'Windows_NT', string>
68-
6976
/**
7077
* Returns `'Linux'` on Linux, `'Darwin'` on macOS, and `'Windows_NT'` on Windows.
7178
*/
72-
async function type(): Promise<OsType> {
73-
return invokeTauriCommand<OsType>({
79+
async function type(): Promise<
80+
LiteralUnion<'Linux' | 'Darwin' | 'Windows_NT', string>
81+
> {
82+
return invokeTauriCommand<string>({
7483
__tauriModule: 'Os',
7584
message: {
7685
cmd: 'type'
7786
}
7887
})
7988
}
8089

81-
type Arch = LiteralUnion<
82-
| 'x86'
83-
| 'x86_64'
84-
| 'arm'
85-
| 'aarch64'
86-
| 'mips'
87-
| 'mips64'
88-
| 'powerpc'
89-
| 'powerpc64'
90-
| 'riscv64'
91-
| 's390x'
92-
| 'sparc64',
93-
string
94-
>
95-
9690
/**
9791
* Returns the operating system CPU architecture for which the tauri app was compiled. Possible values are `'x86'`, `'x86_64'`, `'arm'`, `'aarch64'`, `'mips'`, `'mips64'`, `'powerpc'`, `'powerpc64'`, `'riscv64'`, `'s390x'`, `'sparc64'`
9892
*/
99-
async function arch(): Promise<Arch> {
100-
return invokeTauriCommand<Arch>({
93+
async function arch(): Promise<
94+
LiteralUnion<
95+
| 'x86'
96+
| 'x86_64'
97+
| 'arm'
98+
| 'aarch64'
99+
| 'mips'
100+
| 'mips64'
101+
| 'powerpc'
102+
| 'powerpc64'
103+
| 'riscv64'
104+
| 's390x'
105+
| 'sparc64',
106+
string
107+
>
108+
> {
109+
return invokeTauriCommand<string>({
101110
__tauriModule: 'Os',
102111
message: {
103112
cmd: 'arch'
@@ -118,4 +127,3 @@ async function tempdir(): Promise<string> {
118127
}
119128

120129
export { EOL, platform, version, type, arch, tempdir }
121-
export type { Platform, OsType, Arch }

tooling/cli.rs/src/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl Build {
8787
logger.log(format!("Running `{}`", before_build));
8888
#[cfg(target_os = "windows")]
8989
execute_with_output(
90-
&mut Command::new("cmd")
90+
Command::new("cmd")
9191
.arg("/C")
9292
.arg(before_build)
9393
.current_dir(app_dir())
@@ -96,7 +96,7 @@ impl Build {
9696
.with_context(|| format!("failed to run `{}` with `cmd /C`", before_build))?;
9797
#[cfg(not(target_os = "windows"))]
9898
execute_with_output(
99-
&mut Command::new("sh")
99+
Command::new("sh")
100100
.arg("-c")
101101
.arg(before_build)
102102
.current_dir(app_dir())

0 commit comments

Comments
 (0)