Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefix trivial issues when enabling no-unchecked-record-access for client packages #23432

Merged
merged 8 commits into from
Jan 6, 2025

Conversation

RishhiB
Copy link
Contributor

@RishhiB RishhiB commented Jan 2, 2025

Prefix trivial issues when enabling no-unchecked-record-access for client packages
AB#25378

@github-actions github-actions bot added area: dds Issues related to distributed data structures area: dds: sharedstring area: driver Driver related issues area: loader Loader related issues area: runtime Runtime related issues area: tests Tests to add, test infrastructure improvements, etc base: main PRs targeted against main branch labels Jan 2, 2025
@RishhiB RishhiB marked this pull request as ready for review January 3, 2025 16:57
@Copilot Copilot bot review requested due to automatic review settings January 3, 2025 16:57
@RishhiB RishhiB requested review from a team as code owners January 3, 2025 16:57

Choose a reason for hiding this comment

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

Copilot reviewed 12 out of 27 changed files in this pull request and generated no comments.

Files not reviewed (15)
  • packages/drivers/odsp-driver/src/test/createNewUtilsTests.spec.ts: Evaluated as low risk
  • packages/drivers/odsp-driver/src/compactSnapshotParser.ts: Evaluated as low risk
  • packages/drivers/odsp-driver/src/odspDocumentStorageServiceBase.ts: Evaluated as low risk
  • packages/drivers/odsp-driver/src/odspDriverUrlResolver.ts: Evaluated as low risk
  • packages/dds/sequence/src/revertibles.ts: Evaluated as low risk
  • packages/drivers/odsp-driver/src/WriteBufferUtils.ts: Evaluated as low risk
  • packages/drivers/odsp-driver/src/test/prefetchSnapshotTests.spec.ts: Evaluated as low risk
  • packages/drivers/odsp-driver/src/test/getUrlAndHeadersWithAuth.spec.ts: Evaluated as low risk
  • packages/dds/map/src/test/mocha/directory.spec.ts: Evaluated as low risk
  • packages/runtime/container-runtime/src/test/containerRuntime.spec.ts: Evaluated as low risk
  • packages/drivers/replay-driver/src/storageImplementations.ts: Evaluated as low risk
  • packages/runtime/id-compressor/src/test/idCompressor.spec.ts: Evaluated as low risk
  • packages/drivers/routerlicious-driver/src/documentServiceFactory.ts: Evaluated as low risk
  • packages/dds/merge-tree/src/test/beastTest.spec.ts: Evaluated as low risk
  • packages/loader/driver-utils/src/test/summaryCompresssionTester.spec.ts: Evaluated as low risk
@@ -263,7 +263,8 @@ export class DataVisualizerGraph
this.visualizers[sharedObject.attributes.type] ?? visualizeUnknownSharedObject;

// Create visualizer node for the shared object
const editorFunction = this.editors[sharedObject.attributes.type];
const editorFunction: EditSharedObject | undefined =
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm confused, how can this compile... we're saying that the variable here now might be undefined, but we're still using it without any additional checks in places that do not accept undefined (e.g. line 272(. And I see that TS is even saying that at that point the type is just EditSharedObject, but we never did anything to prevent undefined 🤔 . What am I missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea, weird, VisualizerNode accepts a strict EditSharedObject, but its not erroring when I type editorFunction to EditSharedObject | undefined andpass it in. what do you advise me to do? assert this? Non null assert this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok I think I found the reason. The type of this.editors is

export interface SharedObjectEditors {
	[k: SharedObjectType]: EditSharedObject;
}

Notably, EditSharedObject and not EditSharedObject | undefined. So even after adding | undefined to the variable, TS is inferring that what we assigned to it can only be a EditSharedObject, never undefined, so code further down doesn't complain. See this similar example in TSPlayground.

I think the typing of the index-property in SharedObjectEditors is probably incorrect and should have | undefined as well, but we should address that separately. I'll approve the devtools changes.

Copy link
Contributor

Choose a reason for hiding this comment

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

cc @Josmithr , FYI

Copy link
Contributor

@jason-ha jason-ha Jan 7, 2025

Choose a reason for hiding this comment

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

As it turns out, TypeScript is basically ignoring this additional, explicit type annotation. It thinks that the assignment is more narrow (excludes undefined) and thus these are not useful and even misleading in many cases. If the TS option noUncheckedIndexAccess was used, then we'd be okay. For a poignant example, see https://github.com/microsoft/FluidFramework/pull/23423/files#r1906075392.
(just want Alex said)
So, these are all reverted in the revert PR.

Copy link
Contributor

@alexvy86 alexvy86 left a comment

Choose a reason for hiding this comment

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

Approving for devtools.

@RishhiB RishhiB merged commit 88caebd into microsoft:main Jan 6, 2025
34 checks passed
{ value: [nextVal.value[0], nextVal.value[1].value], done: false };
{ value: [nextVal.value[0], nextVal.value[1]?.value], done: false };
Copy link
Contributor

Choose a reason for hiding this comment

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

@RishhiB, @alexvy86, @Abe27342, why do we like this change?
First, the type of nextVal.value[1] is ILocalValue in all type evaluation cases. There is no chance of undefined here (assuming typing is accurate).
Second, this changes the runtime behavior. Before we would throw here if nextVal.value[1] was actually undefined; now the undefined element is stored in the result.

Copy link
Contributor

Choose a reason for hiding this comment

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

The fact that there was a complaint appears to be a failing of the linter rule. I suspect it doesn't recognize tuples.

Copy link
Contributor

Choose a reason for hiding this comment

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

My review was only for the devtools changes. I agree in this case the change in runtime behavior is reason enough to undo this bit and the similar ones throughout the PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

I've opened #23486

Comment on lines 341 to +343
revertible.intervals.push({
intervalId: interval.getIntervalId(),
label: interval.properties.referenceRangeLabels[0],
label: interval.properties.referenceRangeLabels?.[0],
Copy link
Contributor

Choose a reason for hiding this comment

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

@RishhiB, @alexvy86, @Abe27342, I don't know why TypeScript allows this. label is typed are string. The ? could allow undefined. Perhaps TypeScript is thinking referenceRangeLabels is always defined and then ignoring the ?.
This doesn't look like an okay code pattern. Either we assert with ! (which is removed at transpile) or do something to handle undefined case. Silent storing of undefined for a string appears incorrect.

jason-ha added a commit to jason-ha/FluidFramework that referenced this pull request Jan 7, 2025
…cord-access for client packages (microsoft#23432)"

This reverts commit 88caebd where `?` was used to address linter defect. Many of the test uses of `?` in original change are permissible as there is a follow-up assertion that will fail. But those were not separated during revert.

Changes from original commit that used expected safe adjustments are retained.
jason-ha added a commit to jason-ha/FluidFramework that referenced this pull request Jan 7, 2025
…rd-access for client packages (microsoft#23432)"

This reverts commit 88caebd where `T | undefined` was used to address linter defect. TypeScript will narrow without `undefined` without noUncheckedIndexAccess enabled. So remove ineffective pattern.

The only files that retained changes are:
  packages/dds/merge-tree/src/test/beastTest.spec.ts
  packages/drivers/odsp-driver/src/WriteBufferUtils.ts
  packages/runtime/id-compressor/src/test/idCompressor.spec.ts
  packages/tools/fetch-tool/src/fluidFetchSnapshot.ts
jason-ha added a commit that referenced this pull request Jan 8, 2025
…23486)

Partially revert "Prefix trivial issues when enabling
no-unchecked-record-access for client packages (#23432)"

This reverts commit 88caebd where:
1. `?` was used to address linter defect. TypeScript appears to mostly
ignore that these cases may lead to `undefined` result which is not
accepted per type specifications. Use of `?` is thus a behavior change
that will shift point of failure away from where it could first be
detected - revert those behavior changes. (Many of the test uses of `?`
in original change are permissible as there is a follow-up assertion
that will fail. But those were not separated during revert.)

2. `T | undefined` was used to address linter defect. TypeScript will
narrow without `undefined` without `noUncheckedIndexAccess` enabled.

The only files that retained changes from original commit are:
- packages/dds/merge-tree/src/test/beastTest.spec.ts
- packages/drivers/odsp-driver/src/WriteBufferUtils.ts
- packages/runtime/id-compressor/src/test/idCompressor.spec.ts
- packages/tools/fetch-tool/src/fluidFetchSnapshot.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: dds: sharedstring area: dds Issues related to distributed data structures area: driver Driver related issues area: loader Loader related issues area: odsp-driver area: runtime Runtime related issues area: tests Tests to add, test infrastructure improvements, etc base: main PRs targeted against main branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants