How should a missing project reference be programmatically removed? ('removeReference()' exits with an error)

10 views (last 30 days)
MATLAB R2023a Update 6
Normally, removeReference() can be used to remove a project reference from the top-level project. When I try to use this function on a stale project reference, the function errors with the following error text (redacted local file paths):
>> removeReference(matlab.project.rootProject, stale_project_path)
Error using matlab.internal.project.japi.Project/removeReference
The file or folder "<stale_project_path value>" does not exist.
Error in matlab.project.Project/removeReference
I've also tried using the matlab.project.ProjectReference object directly:
>> broken_ref = matlab.project.rootProject().ProjectReferences(1)
ProjectReference with properties:
...
>> removeReference(matlab.project.rootProject, broken_ref)
Error using matlab.internal.project.japi.Project/removeReference
The file or folder "<broken_ref.File value>" does not exist.
Error in matlab.project.Project/removeReference
Removing the stale project with the GUI works fine, so it's strange that removeReference() refuses to remove the broken project reference.
As a weird workaround, if I create the folder for the stale project reference before trying to remove the reference, I can then call removeReference() without getting an error. I just need to follow up and delete the dummy project folder afterwards.
Example workaround snippet:
broken_ref = matlab.project.rootProject().ProjectReferences(1)
% Create a temporary empty directory at the missing project location
mkdir(broken_ref.StoredLocation)
% Remove broken project reference
removeReference(matlab.project.rootProject, broken_ref)
% Clean up the temporary directories
rmdir(broken_ref.StoredLocation)
tree_path = fileparts(broken_ref.StoredLocation)
% Walk up the directory tree,
% deleting the empty temporary directories as we go.
try
while strlength(tree_path) ~= 0
rmdir(tree_path)
tree_path = fileparts(tree_path)
end
catch
disp("Aborting the deletion of remaining path parts since a directory (""" + tree_path + """) was not empty.")
disp("Therefore, any empty directories that were created during this script should have been deleted while existing, non-empty directories should be untouched.")
end
disp("Successfully removed broken project reference: """ + broken_ref.StoredLocation + """")
% Workspace variable cleanup
clear tree_path
clear broken_ref
For now, the workaround snippet is good enough for me, but it's weird that removeReference() can't do what the respective GUI feature can do and for what seems to me a valid use-case.

Answers (1)

Aditya
Aditya on 30 Jul 2024
Hi Kyle,
This issue has been resolved in MATLAB release R2024a. Updating to this version should allow you to programmatically remove missing project references without encountering the error.

Categories

Find more on Projects in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!