Skip to content

Conversation

pjBooms
Copy link
Collaborator

@pjBooms pjBooms commented Jul 4, 2025

Used by Compose Hot Reload to mark a related accessor as dirty if the hash is changed after reloading of new accessors.

Fixes CMP-8513

Release Notes

N/A

@pjBooms pjBooms requested a review from terrakok July 4, 2025 14:20
@pjBooms pjBooms force-pushed the pjBooms/resource-accessor-annotations branch from 209913f to d8f7123 Compare July 4, 2025 16:20
@pjBooms pjBooms force-pushed the pjBooms/resource-accessor-annotations branch from d8f7123 to 700d707 Compare August 6, 2025 16:00
@pjBooms pjBooms requested a review from sellmair August 6, 2025 16:03
@pjBooms pjBooms force-pushed the pjBooms/resource-accessor-annotations branch from 700d707 to 8bbf398 Compare August 7, 2025 10:06
Copy link
Member

@sellmair sellmair left a comment

Choose a reason for hiding this comment

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

Looks really promising to me!
Here are two concerns that I have:

  1. I am curious about the feature flag; it seems to bring additional complexity and integration problems. I assume that the argument is binary size, but I fail to see how a few bytes compared to the actual resource would make a difference.

  2. I am little concerned about the use of hash functions and the 32-bit hashes potentially colliding. I would be more relaxed if we could use 64bit and proper hashes (but thsi can be done later)

Copy link
Member

@terrakok terrakok left a comment

Choose a reason for hiding this comment

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

LGTM but I'd like to change tests a bit

accessorBuilder.addAnnotation(
AnnotationSpec.builder(resourceContentHashAnnotationClass)
.useSiteTarget(AnnotationSpec.UseSiteTarget.DELEGATE)
.addMember("%L", items.fold(0){acc, item -> ((acc * 31) + item.contentHash)}).build()
Copy link
Member

Choose a reason for hiding this comment

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

fix indentions, please

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

@@ -110,7 +110,8 @@ internal abstract class GenerateResourceAccessorsTask : IdeaImportTask() {
}

val type = ResourceType.fromString(typeString) ?: error("Unknown resource type: '$typeString'.")
return listOf(ResourceItem(type, qualifiers, file.nameWithoutExtension.asUnderscoredIdentifier(), path))
return listOf(ResourceItem(type, qualifiers, file.nameWithoutExtension.asUnderscoredIdentifier(), path,
file.readBytes().contentHashCode()))
Copy link
Member

Choose a reason for hiding this comment

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

let's put every parameter on a new line?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

@@ -141,7 +142,8 @@ internal abstract class GenerateResourceAccessorsTask : IdeaImportTask() {
path: Path
): ResourceItem {
val record = ValueResourceRecord.createFromString(recordString)
return ResourceItem(record.type, qualifiers, record.key.asUnderscoredIdentifier(), path, offset, size)
return ResourceItem(record.type, qualifiers, record.key.asUnderscoredIdentifier(), path,
record.content.hashCode(), offset, size)
Copy link
Member

Choose a reason for hiding this comment

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

here as well

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

@@ -578,6 +578,17 @@ class ResourcesTest : GradlePluginTestBase() {
}
}

@Test
fun testGeneratedAccessorsAnnotatedWithResourceContentHash(): Unit = with(testProject("misc/commonResourcesAnnotatedWithResourceContentHash")) {
Copy link
Member

Choose a reason for hiding this comment

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

Instead of the new test project, we discussed using an existed one (commonResources). You just need to add new expected directory: https://github.com/JetBrains/compose-multiplatform/tree/master/gradle-plugins/compose/src/test/test-projects/misc/commonResources

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

Used by Compose Hot Reload to mark a related accessor as dirty if the hash is changed after reloading of new accessors.
@pjBooms pjBooms force-pushed the pjBooms/resource-accessor-annotations branch 2 times, most recently from a243779 to ffd1d57 Compare August 18, 2025 16:38
Comment on lines +18 to +32
@delegate:ResourceContentHash(1_620_038_668)
internal val Res.drawable._3_strange_name: DrawableResource by lazy {
DrawableResource("drawable:_3_strange_name", setOf(
ResourceItem(setOf(), "${MD}drawable/3-strange-name.xml", -1, -1),
))
}

@delegate:ResourceContentHash(1_620_038_668)
internal val Res.drawable.camelCaseName: DrawableResource by lazy {
DrawableResource("drawable:camelCaseName", setOf(
ResourceItem(setOf(), "${MD}drawable/camelCaseName.xml", -1, -1),
))
}

@delegate:ResourceContentHash(1_620_038_668)
Copy link
Member

Choose a reason for hiding this comment

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

Is it expected that hashes are equal?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As underlying files seem to be the same by content then yes, the hashes will be equal.

@pjBooms pjBooms force-pushed the pjBooms/resource-accessor-annotations branch from ffd1d57 to 81524ae Compare August 18, 2025 17:16
Comment on lines 127 to 131
return if (extension.lowercase() == "xml") {
// Text files have different line endings on Windows/*nix,
// so let's ignore them in the hashcode calculation
Files.readAllLines(toPath()).joinToString().hashCode()
} else {
Copy link
Member

Choose a reason for hiding this comment

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

sorry, I don't understand it. how does it affect our usecase?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Tests on Windows fail without this because when xml files are checked out they are not equal by content with the same files on Linux/Mac (because of different line endings).

Copy link
Member

Choose a reason for hiding this comment

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

the same should happen for any text files, right? I think a correct way is to fix tests instead of hacks here

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok, done

@pjBooms pjBooms force-pushed the pjBooms/resource-accessor-annotations branch from b74756c to 1892dba Compare August 20, 2025 08:49
Copy link
Member

@terrakok terrakok left a comment

Choose a reason for hiding this comment

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

Could you check how the build time is being changed?
I see, we count hashes even without the flag.
A project for the check: https://github.com/terrakok/fluentui-system-icons

to run the demo ./gradlew :demo:run

@pjBooms
Copy link
Collaborator Author

pjBooms commented Aug 20, 2025

Could you check how the build time is being changed?

No differences noted: 1.9.0-beta03 and this version both build around 37s on my machine after ./gradlew clean

@pjBooms pjBooms merged commit ef3ba2e into master Aug 20, 2025
13 checks passed
@pjBooms pjBooms deleted the pjBooms/resource-accessor-annotations branch August 20, 2025 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants