Skip to content

feat: add a new option to remove unused commands#12890

Merged
lucasfernog merged 41 commits intotauri-apps:devfrom
Legend-Master:remove-unused-commands
Mar 15, 2025
Merged

feat: add a new option to remove unused commands#12890
lucasfernog merged 41 commits intotauri-apps:devfrom
Legend-Master:remove-unused-commands

Conversation

@Legend-Master
Copy link
Contributor

@Legend-Master Legend-Master commented Mar 4, 2025

Reference: #12820 (comment)

Introducing a new config option build > removeUnusedCommands to let the tauri-cli pass in an environment variable to let the build script of tauri-plugin and tauri to generate a list of allowed commands from the ACL, this will then be used by the generate_handler macro to remove unused commands

Note: this won't be accounting for dynamically added ACLs so make sure to check it when using this

Some results:

  • Release build of the helloworld example in the repo gone from 3.92 MB to 1.91 MB on Windows (it doesn't use any plugin commands, so the improvement is quite big, also the basic executable contains the image dependency, it's around 2.67 MB if that's removed, but still a great improvement!)

@github-project-automation github-project-automation bot moved this to 📬Proposal in Roadmap Mar 4, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Mar 4, 2025

Package Changes Through b813b76

There are 12 changes which include tauri with minor, tauri-runtime with minor, tauri-runtime-wry with minor, tauri-utils with minor, tauri-cli with minor, @tauri-apps/cli with minor, @tauri-apps/api with minor, tauri-bundler with patch, tauri-build with minor, tauri-codegen with minor, tauri-macros with minor, tauri-plugin with minor

Planned Package Versions

The following package releases are the planned based on the context of changes in this pull request.

package current next
@tauri-apps/api 2.3.0 2.4.0
tauri-utils 2.2.0 2.3.0
tauri-bundler 2.2.4 2.2.5
tauri-runtime 2.4.0 2.5.0
tauri-runtime-wry 2.4.1 2.5.0
tauri-codegen 2.0.5 2.1.0
tauri-macros 2.0.5 2.1.0
tauri-plugin 2.0.5 2.1.0
tauri-build 2.0.6 2.1.0
tauri 2.3.1 2.4.0
@tauri-apps/cli 2.3.1 2.4.0
tauri-cli 2.3.1 2.4.0

Add another change file through the GitHub UI by following this link.


Read about change files or the docs at github.com/jbolda/covector

@WSH032
Copy link
Contributor

WSH032 commented Mar 4, 2025

Hi @Legend-Master (I stumbled upon this PR 😇).

Can we implement this feature in build.rs or as cargo features instead of tauri-cli?

Sometimes I only use cargo build --features tauri/custom-protocol instead of tauri build.

ref: #4670 (comment)

Additionally, the regular Tauri development process heavily relies on Tauri-Cli to generate executables. However, for Pyo3 bindings, dynamic libraries (.so/.dll/.dylib) are required, so we cannot use Tauri-Cli and must use cargo build instead.

@Legend-Master
Copy link
Contributor Author

Legend-Master commented Mar 4, 2025

I have tried it first before this and unfortunately since we can't run the build script before all the macros in plugins, it's not really viable

I could expose the command for generating the used commands though so that you can run it first and then exposing it to the environment manually

@WSH032
Copy link
Contributor

WSH032 commented Mar 4, 2025

I could expose the command for generating the used commands though so that you can run it first and then exposing it to the environment manually.

In some cases, we don't even have tauri-cli 😥, and cargo is the only tool available.

since we can't run the build script before all the macros in plugins

Can we set an environment variable pointing to the capabilities files and then parse the allowed commands in the generate_handler! macro within the plugins?

This way, we wouldn't need to rely on tauri-cli to generate allowed-commands.json.

When I want to enable this feature, I will manually set this environment variable pointing to the capabilities.

(I'm not very familiar with this part of Tauri, so my idea might be wrong)

@Legend-Master
Copy link
Contributor Author

In some cases, we don't even have tauri-cli 😥, and cargo is the only tool available.

I would also like to make those features not relying on the tauri cli if possible

Can we set an environment variable pointing to the capabilities files and then parse the allowed commands in the generate_handler! macro within the plugins?

Actually good idea, I'll give it a try when I got time, but instead of doing it in the macro, I think in plugins's build script should be better? (I'll see when I get there)

My current approach is not that great as it relies on the build scripts to generate the acl manifest first, so definitely something I would like to improve as well

(I'm not very familiar with this part of Tauri, so my idea might be wrong)

To be honest, I'm not very familiar with acl and macro related things either, so any suggestion would be much appreciated

@Legend-Master Legend-Master marked this pull request as ready for review March 6, 2025 07:32
@Legend-Master Legend-Master requested review from a team as code owners March 6, 2025 07:32
@Legend-Master
Copy link
Contributor Author

Legend-Master commented Mar 15, 2025

Well, it still removes the app commands if there're inline plugins but no app manifest right now (app-menu:default seems to be also included through default in the __app-acl__ entry)

I get it now, it's because of UNC paths...

Also I would like to push these thing to the json file as we're already reading and writing from there

I have gone ahead and did that

// when there's no app manifest / inlined plugins we should skip generating the allowed command list
// which then skips the removal of unused commands

I have also moved this to inside of generate_allowed_commands which will help removing the file to reflect potential edits

@lucasfernog This should be working now, tested locally with the api example with some configurations and all seems to work fine

@Legend-Master
Copy link
Contributor Author

Legend-Master commented Mar 15, 2025

Actually, we should not skip this when permissions_map is empty in generate_allowed_commands, that could just mean we didn't specify any commands so we need to remove everything

@Legend-Master
Copy link
Contributor Author

Actually, we should not skip this when permissions_map is empty in generate_allowed_commands, that could just mean we didn't specify any commands so we need to remove everything

Sorry, it was right, it's about the permissions, I thought it was capabilities somehow 😂

lucasfernog
lucasfernog previously approved these changes Mar 15, 2025
Copy link
Member

@lucasfernog lucasfernog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good! 🔥

Co-authored-by: Fabian-Lars <github@fabianlars.de>
@lucasfernog lucasfernog merged commit 013f8f6 into tauri-apps:dev Mar 15, 2025
27 checks passed
@github-project-automation github-project-automation bot moved this from 📬Proposal to 🔎 In audit in Roadmap Mar 15, 2025
@Legend-Master
Copy link
Contributor Author

I feel like we shouldn't apply it for dev as that would likely cause rebuilds due to different environment variables from rust analyzer, and I don't think this is useful in dev anyways

@1111mp
Copy link
Contributor

1111mp commented Mar 19, 2025

@Legend-Master Your work is great, thank you. ❤️

I have a question I hope you can answer: Is this determined by whether the user explicitly provides the required permissions for the command when using the plugin to determine whether it needs to be added to allowed-commands.json. And is it possible to remove unused command in the project itself without using plugins?

Thank you again for your contribution.

@Legend-Master
Copy link
Contributor Author

@Legend-Master Your work is great, thank you. ❤️

❤️

Is this determined by whether the user explicitly provides the required permissions for the command

Yes, but it should already be provided by your capability files that you don't need any additional configs

using the plugin to determine whether it needs to be added to allowed-commands.json

I'm not sure I get what plugin do you mean by that, but this feature would work with or without the plugins (by plugin I mean for example tauri-plugin-updater), the build script (both tauri, tauri-plugin, tauri-build crates) will generate the allowed-commands.json file based on your capability files separately for each crate

root_dir: PathBuf,
) -> Result<(Value, Option<PathBuf>), ConfigError> {
let mut config: Value = parse_value(target, root_dir.join("tauri.conf.json"))?.0;
pub fn read_from(target: Target, root_dir: &Path) -> Result<(Value, Vec<PathBuf>), ConfigError> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Legend-Master Does this crate not follow semantic versioning? Changing root_dir: PathBuf to root_dir: &Path is a breaking change. My library depends on both tauri and tauri-utils, and this change caused a compilation failure when upgrading to tauri v2.4.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this crate not follow semantic versioning?

No, But I think we should still note that in the release note (honestly we rarely did it for tauri-utils and tauri-runtime, but maybe we should as people started using them)

https://docs.rs/tauri-utils/latest/tauri_utils/config/index.html#stability
This is a core functionality that is not considered part of the stable API. If you use it, note that it may include breaking changes in the future.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These items are intended to be non-breaking from a de/serialization standpoint only. Using and modifying existing config values will try to avoid breaking changes, but they are free to add fields in the future - causing breaking changes for creating and full destructuring.

This documentation misled me into thinking that only Config is unstable (I mistakenly thought other APIs were stable 😂)


I am currently using these APIs to dynamically load tauri.conf.json at runtime. I want to know if it is necessary to pin the version of tauri-utils?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This documentation misled me into thinking that only Config is unstable (I mistakenly thought other APIs were stable 😂)

I'm not familiar with this to be honest, not sure if other things are stable or we just didn't put the stability notice at the right place

I am currently using these APIs to dynamically load tauri.conf.json at runtime. I want to know if it is necessary to pin the version of tauri-utils?

I would say yes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it is necessary to pin the version of tauri-utils

I would say yes

Thanks! It's okay to me.

WSH032 added a commit to pytauri/pytauri that referenced this pull request Mar 23, 2025
github-merge-queue bot pushed a commit to pytauri/pytauri that referenced this pull request Mar 23, 2025
* fix(pytauri-wheel/sdist): do not use `path` dependencies in `Cargo.toml`

* fix: pin minor version for unstable `tauri-utils`

ref: <tauri-apps/tauri#12890 (comment)>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 🔎 In audit

Development

Successfully merging this pull request may close these issues.

5 participants