replace_self

Replace or delete the currently running executable on Linux, macOS, and Windows.

self-update
self-delete
native
moon add justjavac/replace_self@0.1.4
Download zip
Author
Version
0.1.4
License
MIT
Last updated
last month
Downloads
29

Dependencies

README

#replace_self

Cross-platform helpers for replacing or deleting the currently running native executable.

match @replace_self.replace_self("/tmp/app.next") {
Ok(()) => ()
Err(error) => println(error)
}

delete_self() uses the same platform rules and returns Result[Unit, ReplaceSelfError].

#
Platform

pub enum Platform {
Windows
MacOS
Linux
Unsupported
} derive(Eq,
Debug
)

Supported host platforms for self-replacement operations.
impl Show for Platform

#
ReplaceSelfError

pub enum ReplaceSelfError {
EmptyReplacementPath
RelativeReplacementPath(String)
ReplacementMatchesCurrentExecutable(String)
ExecutablePathUnavailable
UnsupportedPlatform(Platform)
NativeFailure(String, String)
} derive(Eq,
Debug
)

Errors returned by replace_self package operations.

Validation failures report incorrect inputs supplied by the caller. Discovery failures report missing runtime information such as the current executable path. NativeFailure wraps an operating-system specific failure message produced by the native shim.

#
delete_self

fn delete_self() -> Result[Unit, ReplaceSelfError]

Deletes the currently running executable.

Use this when a process should remove its own executable file after it has finished running, such as a one-shot bootstrapper or an uninstall helper. This is useful when the executable should clean up its on-disk image as part of its own shutdown flow.

Returns

Returns Ok(()) after the file has been unlinked on Unix platforms, or after the deletion helper has been scheduled successfully on Windows. The function does not report whether the current process has already exited; it only reports whether the delete operation or delayed delete setup succeeded.

Platform behavior

  • Linux and macOS unlink the file immediately.
  • Windows launches a detached helper script that waits for the process to exit, then removes the executable file.

Notes

  • On Unix hosts, unlinking removes the directory entry immediately even though the current process may continue running until it exits.
  • On Windows, a successful return means deletion was only scheduled successfully. Callers should exit soon after Ok(()) so the helper can remove the file.

Errors

Returns Err(ReplaceSelfError) when:

  • ExecutablePathUnavailable: the runtime cannot determine the current executable path
  • UnsupportedPlatform: the host platform is unsupported by the native shim
  • NativeFailure: the operating system rejects the deletion request

Example

match @replace_self.delete_self() {
Ok(()) => ()
Err(error) => println(error)
}

#
replace_self

fn replace_self(new_executable : String) -> Result[Unit, ReplaceSelfError]

Replaces the currently running executable with new_executable.

Use this when an application has already downloaded a new binary and wants the current process image on disk to be swapped for that replacement. The replacement file is treated as the source of truth for the next launch of the program.

Arguments

  • new_executable: absolute path to the replacement executable file. Leading and trailing whitespace is trimmed before validation. The replacement file must already exist and must not be the same path as the current executable.

Returns

Returns Ok(()) after the replacement has completed on Unix platforms, or after the replacement helper has been scheduled successfully on Windows. A successful result never includes the final executable path because the current executable location is always the destination.

Platform behavior

  • Linux and macOS perform the replacement immediately with an atomic rename.
  • Windows launches a detached helper script, then completes the replacement after the current process exits and the executable file is unlocked.

Notes

  • On Unix hosts, the replacement source path is moved into the current executable path, so the original source path typically disappears after a successful replacement.
  • On Windows, a successful return means the replacement was only scheduled successfully. Callers should exit soon after Ok(()) so the helper can take over and finish the move.

Errors

Returns Err(ReplaceSelfError) when:

  • EmptyReplacementPath: the path is empty after trimming whitespace
  • RelativeReplacementPath: the path is not absolute for the current platform
  • ReplacementMatchesCurrentExecutable: the replacement path equals the current executable path
  • ExecutablePathUnavailable: the runtime cannot determine the current executable path
  • UnsupportedPlatform: the host platform is unsupported by the native shim
  • NativeFailure: the operating system rejects the replacement request

Example

match @replace_self.replace_self("/tmp/app.next") {
Ok(()) => ()
Err(error) => println(error)
}

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io